0

我在firebird 1.5数据库中有两个表,表是client和notes,在notes表中,Client表中的每个对应记录可以有多个记录,有时没有。表格的结构类似于

客户

Client_id   name
----------------- 
1           Sam 
2           Lee
3           Steve 
4           Linda 
5           Sue 
6           Jill 
7           Jack 

笔记

Notes_id   client_id   Note 
------------------------------ 
1          1           New
2          1           do not send
3          2           old
4          2           likes
5          4           do not send
6          5           new
7          5           Cats and Dogs
8          5           drives

我想运行一个 select 语句,该语句只返回来自客户端表的记录,其中没有名为“不发送”的注释链接到注释表中的客户端。因此,对于上述示例,select 语句只会从客户端表中返回以下记录。

Client_id   name
----------------- 
2           Lee
3           Steve 
5           Sue 
6           Jill 
7           Jack 

这可能吗?对此的任何帮助将不胜感激。

问候艾伦

4

1 回答 1

1

以下是将完成任务的三个查询:

SELECT
  c.*
FROM
  client c 
WHERE
  NOT EXISTS(SELECT * FROM notes n WHERE n.client_id = c.client_id 
    AND n.note = 'do not send')

或者

SELECT
  c.*, n.client_id
FROM
  client.c LEFT JOIN
    (SELECT client_id FROM notes WHERE note = 'do not send') n
  ON c.client_id = n.client_id
WHERE
  n.client_id IS NULL

或者

SELECT
  c.*
FROM
  client c 
WHERE
  NOT c.client_id IN (SELECT client_id FROM notes n 
    WHERE n.note = 'do not send')
于 2012-08-07T13:56:47.607 回答