Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT question

Status
Not open for further replies.

steijssen

Programmer
Mar 25, 2001
92
BE
Hello,

I have a table "items" like this:

id
item
section
posting_date

Now what I want to do is to make a SELECT query that retrieves the 3 newest "items" for this table, but none of them should belong to the same "section". How do I do this?

Until now I have:
SELECT * FROM items GROUP BY posting_date DESC LIMIT 3

Thanks for your time!!
 
Well first off you need

SELECT * FROM items ORDER BY posting_date DESC LIMIT 3

Order By rather than Group By


getting the 3 to have unique Section values will take a bit more thinking about




G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
How about

Code:
SELECT * 
FROM items As i1 
ORDER BY posting_date DESC LIMIT 1

UNION

SELECT * 
FROM items As i2 
WHERE Section NOT IN ( i1.Section ) 
ORDER BY posting_date DESC LIMIT 1

UNION

SELECT * 
FROM items As i3
WHERE Section NOT IN ( i1.Section, i2.Section )
ORDER BY posting_date DESC LIMIT 1



'ope-that-'elps.



G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
Seems logical, but I get an error "Incorrect use of Union and Order by"...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top