查询两个表,结果与 join like 语句相反 [英] Query two tables with result opposite of join like statement

查看:42
本文介绍了查询两个表,结果与 join like 语句相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是获得一些奖牌的简单问题:):我有三张表:contacts"保存联系人信息,其中每个条目都有一个唯一的 id.第二个表groups"存储每个组的名称和组 ID.最后一个contacts_groups"将每个联系人绑定到一个组,它只有两列,contact_id 和 group_id.

Here is the simple question to gain some medals :) : I have three tables: "contacts" that holds the contacts information where each entry has a unique id. The second table "groups" where is stores the name for each group and the group id. The last "contacts_groups" binds each contact to a group, it has just two columns, contact_id and group_id.

问题是如何在单个语句中编写一个 MySQL 查询,该查询将选择未分配给某个组的所有联系人.换句话说,哪些 id 未列在contacts_groups"表中的联系人?

The question is how do write in a single statement a MySQL query that will select all contacts that are not assigned to a group. In other words contacts which id is not listed in "contacts_groups" table?

推荐答案

select * from contacts c
left outer join contacts_groups cg on c.id = cg.contact_id 
where cg.contact_id is null

这篇关于查询两个表,结果与 join like 语句相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆