首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL查询以获取其列不应与其他表列匹配的表行。

SQL查询以获取其列不应与其他表列匹配的表行。
EN

Stack Overflow用户
提问于 2016-11-07 12:21:43
回答 4查看 50关注 0票数 0

提前谢谢你,

实际上我有两张桌子,两辆车和支票。Carts表包含以下行

代码语言:javascript
运行
复制
id |username  |orderid  | exam_name | price
1  |  Rajesh  | ABC123  |   PMP     |  $60
2  |  Rajesh  | ABC123  |   CPM     |  $70
3  |  David   | ABC789  |  ITIL     |   $80

checks表包含如下行

代码语言:javascript
运行
复制
id |username |order_id | exam      |  price
1    Rajesh  | ABC123  |   PMP     |  $60
2    Rajesh  | ABC123  |   CPM     |  $70

我需要一个carts表的行数据,其orderid列和exam_name列不应该与checks、order_id列和need列匹配。

如下所示:

代码语言:javascript
运行
复制
id |username  |orderid  | exam_name | price
1  |  David   | ABC789  |  ITIL     |   $80
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-11-07 12:23:31

一种方法是not exists

代码语言:javascript
运行
复制
select c.*
from carts c
where not exists (select 1
                  from checks ch
                  where ch.orderid = c.orderid and ch.exam_name = c.exam_name
                 );

一个类似的方法是left join

代码语言:javascript
运行
复制
select c.*
from carts c left join
     checks ch
     on ch.orderid = c.orderid and ch.exam_name = c.exam_name
where ch.orderid is null;

在某些数据库中,您可以使用not in

代码语言:javascript
运行
复制
select c.*
from carts c
where (c.orderid, c.exam_name) not in (select ch.orderid, ch.exam_name from checks ch);
票数 2
EN

Stack Overflow用户

发布于 2016-11-07 12:24:09

代码语言:javascript
运行
复制
select c.*
from carts c
left join checks ch on c.id = ch.id
where ch.id is null;

希望这能解决你的问题。

票数 2
EN

Stack Overflow用户

发布于 2016-11-07 12:34:01

代码语言:javascript
运行
复制
SELECT *
from Carts 
where  exam_name  not in (select  exam       from checks) 
and id  not in (select id  from  checks)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40465076

复制
相关文章

相似问题

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