首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >仅连接值不在表B中的表

仅连接值不在表B中的表
EN

Stack Overflow用户
提问于 2011-09-02 04:10:06
回答 1查看 8K关注 0票数 6

我正在尝试连接2个表,并过滤一个不存在的值。

标题: item_id、Table_Items等

Table_History: item_id,history_id,action,date

对于每一张光盘,都有许多可能的操作(购买、添加、播放、翻录等)每个操作在由item_id链接的table_history中创建一个新行。我正在寻找的查询将返回所有未翻录的光盘。我的查询返回了太多的行,返回了所有内容,因为历史表中的每一项至少有一个条目。

代码语言:javascript
运行
复制
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。如果能帮上忙,我们将不胜感激!谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-02 04:13:53

只需将操作过滤器添加到连接中,然后在where中测试null (反连接

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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 
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7275925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档