PDA

View Full Version : Order By


Calilo
04-27-2006, 06:32 PM
Hi, a simple question

im using php and mysql, but i would like from the sql statement to get the results of a query order by a column and then reorder by another column that is:

i want to select the last four entries accoding to the date.
SELECT * FROM table ORDER BY Date DESC LIMIT 4

but I also want to order those 4 entries i got, according to their values at the order column like this

SELECT * FROM table ORDER BY ordercol ASC LIMIT 4

how can i put this two together?

i thougt of
SELECT * FROM table ORDER BY Date DESC, ordercol ASC LIMIT 4

but it gets the last four entries and then if two entries have the same date it orders them according to the ordercol, how can it do it?

hope i explained myself

thank

Calilo

PremiumBlend
04-28-2006, 12:18 AM
What do you mean: order by a column and then reorder by another column

You want it to retrieve the 4 most recent dates, then what?

PremiumBlend
04-28-2006, 12:20 AM
Ok, I get it now, you want the 4 most recent dates, but not ordered by date, ordered by your "ordercol" field. Is this correct?

PremiumBlend
04-28-2006, 12:34 AM
Give this a try:


SELECT
FROM [table]
WHERE [unique field] IN
(SELECT [unique field] FROM [table name] ORDER BY Date DESC LIMIT 4)
ORDER BY [order column] ASC

I got it to work with MSSQL and i'm not sure how the MySQL syntax should be exactly. Its not the same because MSSQL doesn't use the reserved word "LIMIT", it uses the reserved word "TOP" in the select line. I adjusted it as I believe it's needed. If you want the exact MSSQL command, let me know.

Calilo
04-28-2006, 02:00 AM
Excelent, I didnt know you could do select inside selects. i could find any info cause i wasnt looking for it in the correct way.

Thank you alot, worked like a charma

Calilo :thumbsup: