在一个表中查找未在链接表中多次出现的行

时间:2018-11-02 09:19:44

标签: mysql

我有多个站点,当用户在一个站点上注册时,我们会在多个站点上自动为其创建一个帐户。

我们管理它的“中央”数据库有一个客户表,一个站点表,然后有一个clients_sites表作为链接。

clients table
-------------
id | name
1  | Larry
2  | Curly

sites table
-----------
id | url
1  | http://one.com
2  | http://two.com

clients_sites
--------------
id | clients_id | sites_id
1  | 1          | 1
2  | 1          | 2
3  | 2          | 1

我需要找到一个有效的查询来查找未在每个站点的链接表中列出的客户端。

因此,在此示例中,Curly未在站点2中列出。

是否有一个SQL查询来查找未出现在每个站点的链接表中的客户端?

1 个答案:

答案 0 :(得分:1)

  • 我们可以考虑在Derived Table中使用<button type="button" name="call" id="call" class="btn btn-primary btn-lg call-customer-button"> <span class="glyphicon glyphicon-earphone" aria-hidden="true"></span> Call customer </button> <input type="text" id="phone" name "phone" value="07777 777 777" />,将clients_idsites_id的所有可能的组合
  • 现在,我们可以使用Cross Joinclients_sites,使用“反联接”仅考虑在Left Join表中找不到匹配行的那些“组合”(没有匹配的行)

尝试以下查询:

clients_sites.id IS NULL