我正在尝试连接2个表,并过滤一个不存在的值。
标题: item_id、Table_Items等
Table_History: item_id,history_id,action,date
对于每一张光盘,都有许多可能的操作(购买、添加、播放、翻录等)每个操作在由item_id链接的table_history中创建一个新行。我正在寻找的查询将返回所有未翻录的光盘。我的查询返回了太多的行,返回了所有内容,因为历史表中的每一项至少有一个条目。
SELECT items.item_id, items.title AS Title, items.`author/creator`, history.action
FROM items
LEFT JOIN history ON items.item_id = history.item_id
WHERE history.action NOT LIKE 'Ripped%' GROUP BY items.item_id ORDER BY title我正在使用mySQL。如果能帮上忙,我们将不胜感激!谢谢。
发布于 2011-09-02 04:13:53
只需将操作过滤器添加到连接中,然后在where中测试null (反连接
SELECT items.item_id,
items.title AS title,
items.`author/creator`,
history.ACTION
FROM items
LEFT JOIN history
ON items.item_id = history.item_id
AND history.ACTION LIKE 'Ripped%'
WHERE history.item_id IS NULL
GROUP BY items.item_id
ORDER BY title 你也可以做一个not in
SELECT items.item_id,
items.title AS title,
items.`author/creator`,
history.ACTION
FROM items
LEFT JOIN history
ON items.item_id = history.item_id
WHERE history.item_id NOT IN (SELECT history.item_id
FROM history
WHERE history.ACTION LIKE 'Ripped%')
GROUP BY items.item_id
ORDER BY title https://stackoverflow.com/questions/7275925
复制相似问题