The following query is pulling duplicate site_ids, with me using DISTINCT I can’t figure out why…
SELECT
DISTINCT site_id,
deal_woot.*,
site.woot_off,
site.name AS site_name
FROM deal_woot
INNER JOIN site ON site.id = site_id
WHERE site_id IN (2, 3, 4, 5, 6)
ORDER BY deal_woot.id DESC LIMIT 5
DISTINCTlooks at the entire record, not just the column directly after it. To accomplish what you want, you’ll need to useGROUP BY:Non-working code:
Why doesn’t it work? If you
GROUP BYa column, you should use an aggregate function (such asMINorMAX) on the rest of the columns — otherwise, if there are multiplesite_woot_offs for a givensite_id, it’s not clear to SQL which of those values you want toSELECT.You will probably have to expand
deal_woot.*to list each of its fields.Side-note: If you’re using MySQL, I believe it’s not technically necessary to specify an aggregate function for the remaining columns. If you don’t specify an aggregate function for a column, it chooses a single column value for you (usually the first value in the result set).