views:

66

answers:

3

I have been trying to figure out a different way to complete this task on another question on this website, but think maybe I am making it too difficult.

Here is what I have: Table with, and ImageID, ImageName, GalleryID Another Table with Comments, Author, Date, ImageID

What I want to do is do a query where I find all of the Images that have a galleryID=42. In addition, I would like to grab all of the comments that are associated with each picture (via the ImageID) and concatenate them in a single value. For example:

ImageID: 1234, ImageName: IMG425, GalleryID: 42, Comments: Cool!|||John Smith|||2010-09-06~~Nice shot!|||Richard Clark|||2010-10-01~~I remember this run.|||Susan Edwards|||2010-10-04

I need to concatenate all of the results from the Comments table that are for each image and put them in as a single value, then I can parse them via PHP in the body of my page.

A: 

there's a function in mysql called GROUP_CONCAT i havent really tried it but i think it could help

Good Luck

EDITED:

The query might be something like

SELECT img.id,img.name,img.galleryID, 
   GROUP_CONCAT(com.author,comment.date,com.content 
                ORDER BY comm.date SEPARATOR '|||')
FROM images img JOIN comments com ON img.imageID=com.imageID
GROUP BY img.id,img.name,img.galleryID;

or something like that, but i dont know if group_concat works with joins

pleasedontbelong
I tried this, but it gave me only one result.SELECTGalleryData.ID,GalleryData.FileName,GalleryData.GalleryID,GalleryData.Description,Galleries.GalleryName,Galleries.GalleryFolder,GROUP_CONCAT(Comments.CommentAuthor,Comments.CommentDate, Comments.`Comment` ORDER BY Comments.CommentDate SEPARATOR '|||')FROMGalleryDataINNER JOIN Galleries ON GalleryData.GalleryID = Galleries.GalleryIDLEFT JOIN Comments ON GalleryData.ID = Comments.IDWHERE GalleryData.GalleryID=42
dragboatrandy
have you forgot the last GROUP BY? or you run out of characters =P .. try a simple query to check if group_concat() is your answer, maybe just showing the imageID and the authors of the comments..
pleasedontbelong
A: 

Why not just pull the comments data in a separate query?

While the extra trip to the database isn't ideal, it's probably going to be just as much overhead as concatenating and extracting that data, and separating the queries will make your code less jumbled.

jwiscarson
I tried that. See http://stackoverflow.com/questions/3884347/php-mysql-array-pop-missing-first-value
dragboatrandy
That seems like a non-sequitur in my opinion. Just because your original code contains a flaw (and given the comments on that question, it looks like you have viable options to fixing the original problem). You even say in your question that you may be overcomplicating the issue. In my experience, rewriting straightforward code to brute force a solution just leads to all sorts of angst later on.
jwiscarson
+1  A: 

GROUP_CONCAT() is the way to go, and the other answers are close. KISS.

SELECT
   ImageID, ImageName, GalleryID
   , GROUP_CONCAT(
      CONCAT_WS('|||', Comment.author, Comment.date, Comment.content)
      SEPARATOR '~~'
   ) as comments
FROM
   Images
   JOIN Galleries USING (GalleryID)
   JOIN Comments USING (ImageID)
WHERE
   GalleryID = 42
GROUP BY
   ImageID, ImageName, GalleryID

Note that GROUP_CONCAT() has a very short max length by default. You may have to run

SET group_concat_max_len = 65535;
tandu
The concatenate part works, but it only yields the images that have comments. I want it to show all of the images, but include the comments with the ones that actually have comments. There are 400 or so images with that GalleryID and when I run the query, only 3 show, because those are the ones with comments.
dragboatrandy
Oh sorry. Left join on Comments then :)
tandu