如果右表匹配,则排除记录 [英] Exclude records if right table matches

查看:48
本文介绍了如果右表匹配,则排除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

A:

+--+----+
|id|name|
+--+----+
|0 |foo |
|1 |bar |
|2 |baz |
+-------+

B:

+--+----+
|A |cond|
+--+----+
|0 |X   |
|1 |Y   |
+-------+

其中 B.A 列是 A.id 值.

Where B.A column is A.id value.

当 B.cond = 'X' 时,我想从 A 中选择 B 表中没有匹配项的所有行.

I want to select all row from A where have no match in B table when B.cond = 'X'.

所以,结果应该是:

  • baz

如何用join(或类似的性能方法)编写这个SQL请求?

How to write this SQL request with join (or similar performance method) ?

推荐答案

SELECT
  A.*
FROM
  A
LEFT JOIN
  B
  ON A.id = B.A
  AND B.cond = 'X'
WHERE
  B.A IS NULL

此查询根据您指定的条件连接表,然后仅选择表 B 中没有匹配项的行.

This query joins the tables based on the conditions you specified, and then only selects the rows where there's no match in table B.

这篇关于如果右表匹配,则排除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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