我不是一个真正的开发人员,但我正在开发一个应用程序作为一个副项目,并且已经厌倦了我试图解决的SQL查询。此应用程序包含标记的图像集合。我的表结构看起来像这样:
images
------
image
path
tag_map
------
image
tag
tags
-------
tag
tag_name
favorites
-------
user
image
我的查询适用于标记系统,但是,我没有成功添加收藏夹的查询。目前我的查询如下:
SELECT i.*, IF (c.image IS NOT NULL,1,0) as favorited
FROM (images i, tag_map m, tags t)
LEFT JOIN favorites as c ON c.image = i.image
WHERE m.tag = t.tag
AND (t.tag_name IN ('umbrella'))
AND i.image = m.image
AND c.image = i.image
GROUP BY i.image
HAVING COUNT( i.image ) = 1
虽然此查询有效,但它会排除注释在收藏夹表中有条目的行。以下简化(无标记系统)查询有效:
SELECT i.*, c.*,
IF(c.image IS NOT NULL,1,0) as fave
FROM (images as i)
LEFT JOIN collection as c ON c.image = i.image
任何正确方向的建议或观点都会很棒!谢谢!
答案 0 :(得分:0)
事实证明,一旦我删除了多余的"并且c.image = i.image" 我无意中离开了那里,我的查询就可以了。
工作查询是:
SELECT i.*, IF (c.image IS NOT NULL,1,0) as favorited
FROM (images i, tag_map m, tags t)
LEFT JOIN collections as c ON c.image = i.image
WHERE m.tag = t.tag
AND (t.tag_name IN ('umbrella'))
AND i.image = m.image
GROUP BY i.image
HAVING COUNT( i.image ) = 1