views:

44

answers:

2

I have the following query, its only returning two records when i have three in the database- where am I going wrong?

SELECT items.id, items.link, items.title, items.image, lists.user 
FROM lists, items 
WHERE lists.user = '506161637' AND lists.item = items.id
GROUP BY lists.dated DESC LIMIT 0, 10000;

The data set of a join table is below but its only returning two records... Rows 19 and 17? maybe theres a better group function to use?

        id user item dated
  19 506161637 8 2009-11-19
  18 506161637 6 2009-11-19
  17 506161637 5 2009-11-18
A: 

Remove the GROUP BY clause in your statement. This is grouping the '2009-11-19' records together.

KG
ah yes!!! but how can i sort them?
chris
Use `ORDER BY lists.dated` instead
Ivan Nevostruev
You'll have to choose how you'd like to sort them. Perhaps by User or item.
KG
KG — how come this even executes? What is MySQL doing with the non-aggregated, non-grouped columns?
Larry Lustig
@Larry - since two of those records have the same date, when the GROUP BY lists.dated is specified without actually SELECTing the date, it combines them and, my guess, returns the data associated with the best choice, in this case it looks like the items.id field 19 would be greater than 18.
KG
@Larry - my guess is the engine that is specified for this mysql instance is using some implied aggregation on non-selected fields.
KG
its not working again- im only grabbing 3 records from a table of 4- i tried the query above but also thisSELECT * FROM itemsJOIN lists ON items.id = lists.itemWHERE lists.user =506161637LIMIT 0 , 30same result
chris
A: 

Items 19 and 18 have the same date and since you are grouping by that, they will combine and show the first record.

Jrud