Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MYSQL Query woes

Posted on 2011-10-17
3
Medium Priority
?
251 Views
Last Modified: 2013-12-12
I am at my wits end and could really do with some expert advise. Let me try to describe my issue.

I am trying to generate a PHP table that displays period data. Each year has 13 periods, each period having 4 weeks.

Hence we have a period table with fields

id
period
perioddetails

and a period data table with fields

id
periodnumber (references previous table)
weeknumber (a number from 1 to 4)
status1
status2
...
status30

Period refererences another table which has the perdio name, financial year etc.

However I want to generate a query that will generate a table with the current period (weeks 1 to however many there are) and the last period (4 weeks)

I have been able to generate a routine that can easily pull off the latest period data with the following


SELECT *
FROM mfugi_kpi_perioddata
WHERE period1.periodnumber =
(SELECT id
FROM mfugi_kpi_period
ORDER BY ID DESC
LIMIT 1
)

this simply takes the latest period from the period table, and uses that as a reference to pull off the latest period data. However, I have no clue how to pull of the period before, and also clearly distinguish these results for easy display later on a PHP generated table. I have tried the IN statement just before the nested query, but am told my version of MYSQL does not allow it.

Any pointers would be appreciated. The query above is the closest I have come, I have had many complicated abortive attempts not worth posting.
0
Comment
Question by:McDeathUK
3 Comments
 
LVL 3

Expert Comment

by:saimazz
ID: 36978827
may you write more clearly your problem what you want to get from tables. Is that the current period or some other range? Is there needed to group bay something or special ordering?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36978830
what about:
SELECT *
FROM mfugi_kpi_perioddata
WHERE period1.periodnumber = ( SELECT MAX(id) FROM mfugi_kpi_period )
   OR period1.periodnumber = ( SELECT MAX(id) FROM mfugi_kpi_period WHERE id < ( SELECT MAX(id) FROM mfugi_kpi_period ) )

Open in new window

0
 

Author Closing Comment

by:McDeathUK
ID: 36978861
LOL!

Works like a charm! Thank you so much. I have only done basic testing, but I setup three periods, added a load of data.. and this query successfuly brough off the latest period and data, and the previous data and data.

Thank you
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question