开发者

Joining multiple entries to single entry

I have 2 tables, one lists Properties (Houses, flats etc) and the other table holds image filenames and an ID to link to the relevant Property.

Now, I need to joi开发者_如何学运维n these together, but in a way so that I only get the Property information returned once, and then the X number of image records that are linked.

I have had some success using a LEFT JOIN, in that I can select the records, but it is returning duplicate info for the Property e.g. 1 Property linked to 3 images would return 3 results from the LEFT JOIN query and thusly on my output page, print the details 3 times.

The next part after that is how do I print the Property details (which should now be singular) and the multiple image information?

Hope you can help!


You can use a group_concat function to get a list of the file names. By default, this will only allow you to have 1k of data in the resulting group_concat field.

select prop.*, group_concat(filenames) as files
   from properties prop 
   join images on prop.id = images.prop_id
   where prop.owner_id = 'something'
   group by prop.id

You can tweak the group_concat_max_len property if you need more data in that field.

Then in PHP you would look over resulting query, and you can explode each of the files column, and loop over the resulting array to display the images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜