我想从MySQL数据库中选择新闻稿。
xxxxxxxxxxxxxxxxxxxxxxxxxx
表格:
PressReleaseContent
ID|PressRelease|DomainLanguages|Day|Title|SubTitle|Menu|Text|FileType|Active
PressRelease(多个图片ID,在“图片”字段中用逗号分隔)
ID|Name|Category|News|Groups|Product|Pictures|PictureType|Active
730|14.08.2018 - New Product|txt_pressreleases|0|0|0|5218,5219,5221,5222,5223,5224,5225,5226,5227,5228|press|1
图片
ID|FileName|PictureType|DomainLanguages|Active
5227|downloads/press/1|jpg|1|1
5228|downloads/press/2|jpg|1|1
5229|downloads/press/3|jpg|1|1
xxxxxxxxxxxxxxxxxxxxxxxxxx
查询:
SELECT pr.ID, pr.Name, pr.Category, pr.Pictures, pr.PictureType, prc.ID,
prc.DomainLanguages, prc.Day, prc.Title, prc.SubTitle, prc.Menu, prc.Text,
prc.FileType, pic.FileName
FROM `PressRelease` pr
LEFT JOIN PressReleaseContent prc ON ( pr.ID = prc.PressRelease )
LEFT JOIN Picture pic ON ( FIND_IN_SET( pic.ID, pr.Pictures ) )
WHERE pr.Active =1
#GROUP BY prc.ID
一个新闻稿内容包含多个条目的结果。在以下示例中,ID为2455的新闻稿内容重复了两次,因为新闻稿包含2张图像5227和5228:
...
730|14.08.2018 - new product|txt_pressreleases|5218,5219,5221,5222,5223,5224,5225,5226,5227,5228|press|2455|6|3||Première mondiale pendant|||docx|downloads/press/1|5227
730 | 14.08.2018-新产品| txt_pressreleases | 5218,5219,5221,5222,5223,5224,5225,5226,5227,5228 | press | 2455 | 6 | 3 ||Premièremondiale吊坠||| docx | downloads / press / 2 | 5228 ...
xxxxxxxxxxxxxxxxxxxxxxxxxx
我的问题是,PressRelease表在“图片”字段中包含图片的逗号分隔ID。因此,我使用带有“ FIND_IN_SET”的联接来从“图片”表中接收图片的文件名。这行得通,但是现在对于每个找到的图像我都会得到多个结果。取而代之的是,对于每个新闻稿内容,文件名都按逗号分组的情况,我只希望有一个结果:image1.jpg,image2.jpg,image3.jpg等。
xxxxxxxxxxxxxxxxxxxxxxxxxx
所需的结果,其中图像名称用逗号分隔:
730|14.08.2018 - new product|txt_pressreleases|5218,5219,5221,5222,5223,5224,5225,5226,5227,5228|press|2455|6|3||Première mondiale pendant|||docx|downloads/press/1,downloads/press/2,|5228
xxxxxxxxxxxxxxxxxxxxxxxxxx
只有一个结果结果,用逗号分隔图片名称。这就是我所需要的。
当我将GROUP_CONCAT添加到SELECT时,我得到了:
xxxxxxxxxxxxxxxxxxxxxxxxxx
SELECT pr.ID, pr.Name, pr.Category, pr.Pictures, pr.PictureType, prc.ID, prc.DomainLanguages, prc.Day, prc.Title, prc.SubTitle, prc.Menu, prc.Text, prc.FileType, GROUP_CONCAT(pic.FileName)
xxxxxxxxxxxxxxxxxxxxxxxxxx
结果:
1|01.01.2002 - first news|txt_pressreleases|1|press|1|6|3||Première news|<ul class="jmpdwn">|<a name|docx|downloads/press/1,downloads/press/1, downloads/press/1, downloads/press/1,downloads/press/1, downloads/press/1,...
在主表的第一个数据集和逗号分隔的所有文件名中,我只有1个结果。那我该怎么做呢? 谢谢!