MYSQL Query woes

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.
McDeathUKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

saimazzCommented:
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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
McDeathUKAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.