带有标签的 CONCAT
我有以下查询来选择照片、其标签和一些基本用户信息:
SELECT photos.*, GROUP_CONCAT(tags.tag SEPARATOR ', ') AS photo_tags
FROM photos
INNER JOIN users
ON photos.upload_by = users.user_id
INNER JOIN tags_photos
ON tags_photos.photo_id = photos.photo_id
INNER JOIN tags
ON tags.tag_id = tags_photos.tag_id
WHERE photos.status = '$status'
AND photos.photo_id IN (SELECT photos.photo_id
FROM photos
JOIN tags_photos
ON photos.photo_id = tags_photos.photo_id
JOIN tags
ON tags_photos.tag_id = tags.tag_id)
GROUP BY photos.photo_id
ORDER BY status, upload_date;
它运行良好,唯一的例外是,如果照片没有标签,则不会返回该照片。即使没有标签,我希望它仍然返回照片。
有人有什么建议吗?
I have the following query to select photos, their tags, and some basic user information:
SELECT photos.*, GROUP_CONCAT(tags.tag SEPARATOR ', ') AS photo_tags
FROM photos
INNER JOIN users
ON photos.upload_by = users.user_id
INNER JOIN tags_photos
ON tags_photos.photo_id = photos.photo_id
INNER JOIN tags
ON tags.tag_id = tags_photos.tag_id
WHERE photos.status = '$status'
AND photos.photo_id IN (SELECT photos.photo_id
FROM photos
JOIN tags_photos
ON photos.photo_id = tags_photos.photo_id
JOIN tags
ON tags_photos.tag_id = tags.tag_id)
GROUP BY photos.photo_id
ORDER BY status, upload_date;
It works well, the only exception being that if the photo has no tags, it doesn't return that photo. I'd like it to still return the photo even if there are no tags.
Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定为什么需要在 WHERE 子句中使用 IN 子查询。
Not sure why you'd need the IN subquery in the WHERE clause.
看起来您需要在
tags_photos
和photos
之间进行RIGHT OUTER JOIN
Looks like you need a
RIGHT OUTER JOIN
betweentags_photos
andphotos