My table is called items. It has the columns item_id and source as well as some other columns.
I want to do a select ordered by item_id but I also want to not have any duplicates in the source column of the result.
What’s wrong with this query?
SELECT *
FROM items
WHERE item_section='sp_500'
ORDER BY item_id DESC
GROUP BY source
LIMIT 3
Should work like this:
Why?
1) Filter by
item_section = 'sp_500'2) Collapse multiple items with the same
sourcein aGROUP BY, because:I take the biggest
item_idpersource– seems most plausible and you did not specify.3)
ORDER BY item_id DESCto get the greatest ones and LIMIT 3 (without dupes by now).4)
JOINto the original table to get the whole row for the selecteditem_ids.