0

I'm running this SQL query

$sql = "select images.image, images.comment as feedDescription, 
        customers.fullName, CONCAT('[', GROUP_CONCAT(DISTINCT likes.uid),']') as likes, 
        CONCAT('[', GROUP_CONCAT(DISTINCT CONCAT('{\"userid\":\"', comments.fid, '\", \"comment\":\"', comments.comment, '\"}') separator ','),']') as comments 
        FROM images 
        LEFT JOIN customers on images.client_id = customers.client_id 
        LEFT JOIN likes on images.image = likes.image 
        LEFT JOIN comments on images.image = comments.image 
        WHERE images.fid=:userID 
        ORDER BY images.image LIMIT $offset,$limit";

the only problem is that I am getting only the first row ...

I have images table, customers table (taking the name of the customer by the id i got in the images), likes table (people who did "like" on the image) and comments (people who wrote "comments" on the table)

4
  • 1
    Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it!
    – marc_s
    Aug 28, 2012 at 14:05
  • What's the value of $limit ? are you sure you're not asking for one row (images.fid=:userID ) ? please post an example user data (from all 4 tables) and the output
    – Nir Alfasi
    Aug 28, 2012 at 14:10
  • Hi, Limit is 20 and offset is 0
    – Eyal Zach
    Aug 28, 2012 at 14:14
  • Each user post several images, so for each image, i want to have a row with all the likes and comments it got
    – Eyal Zach
    Aug 28, 2012 at 14:15

1 Answer 1

1

You are using an aggregation function on a query, so MySQL is automatically returning only one row -- the aggregation of all the data.

In other databases, this would produce an error, because you have a mixture of aggregated and non-aggregated columns. This is a (mis)feature of MySQL called "hidden columns".

Add a group by to your query to fix the problem:

group by images.image, images.comment, customers.fullName

Be sure to add this after the WHERE clause and before the ORDER BY.

3
  • 1
    That is correct, but i wouldn't call it a feature. It is bad behaviour to guess what might have been meant if a query is not implemented correctly (like this one)
    – Argeman
    Aug 28, 2012 at 14:47
  • I'd be happy to get it fixed, and understand whats the correct way to do it.
    – Eyal Zach
    Aug 28, 2012 at 14:54
  • btw i'm getting a Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by images.image, images.comment as feedDescription, customers.fullName LIM' after adding the group by as above
    – Eyal Zach
    Aug 28, 2012 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.