我需要哪种类型的加入?

发布于 2024-12-09 20:39:41 字数 186 浏览 0 评论 0原文

我有两张桌子、合同和推销员。 最近我发现数据中有一些错误,有些合同的 salesid 在“salesmen”中找不到,我怀疑是意外删除或某种输入错误。

我应该使用哪个连接来查找所有不“属于”销售员的合同,换句话说,在 salesmen.id 列中找不到contract.salesid。

它应该是一个右外连接,但结果并不正确。

I have 2 tables, contracts and salesmen.
Recently I've discovered some errors in the data, some contracts have salesid's not found in 'salesmen', I suspect an accidental deletion or an input error of some kind.

Which join should I use to find all contracts that dont 'belong' to a salesman, in other words, the contract.salesid not found in the salesmen.id column.

It should be a right outer join but the results arent coming up right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

度的依靠╰つ 2024-12-16 20:39:41

听起来你正在寻找“反加入”。 解释扩展 讨论了在 MySQL 中执行此操作的三种方法:

  • 带有 WHERE __ IS NULL 的左连接
  • 带有子查询的 NOT IN 子句。
  • 带有子选择的 NOT EXISTS 子句。

Sounds like you're looking for an "anti-join". Explain Extended talks about the three ways to do this in MySQL:

  • A left join with a WHERE __ IS NULL
  • A NOT IN clause with a subselect.
  • A NOT EXISTS clause with a subselect.
肥爪爪 2024-12-16 20:39:41

如果您要查找在 salesmen.id 中找不到的 contract.salesid,则可以使用 NOT IN() 而不是联接。

SELECT * FROM contracts WHERE salesid NOT IN (SELECT id FROM salesmen);

上面的代码将返回 contractssalesid 与现有 salesmen.id 不匹配的所有内容。在大型表上使用 NOT IN () 可能比使用 JOIN 慢,但如果您的表不太大,那么通常是更直接的使用方法(在我看来)。

If you're looking for contract.salesid not found in the salesmen.id, you can use NOT IN() rather than a join.

SELECT * FROM contracts WHERE salesid NOT IN (SELECT id FROM salesmen);

The above will return everything from contracts having salesid that matches no existing salesmen.id. Using NOT IN () on large tables can be slower than doing it with a JOIN, but if your tables aren't too large it is usually a more straightforward method to use (in my opinion).

书间行客 2024-12-16 20:39:41
SELECT c.contract_id FROM contract c
LEFT OUTER JOIN salesmen s ON s.salesman_id = c.salesman_id
WHERE c.salesman_id IS NULL

我的猜测是。

SELECT c.contract_id FROM contract c
LEFT OUTER JOIN salesmen s ON s.salesman_id = c.salesman_id
WHERE c.salesman_id IS NULL

would be my guess.

烟花肆意 2024-12-16 20:39:41

左外连接如果您从合同加入到推销员

编辑:订单方式错误

left outer join if you are joining from contracts to salesman

edit: had order around the wrong way

花落人断肠 2024-12-16 20:39:41

外连接确实可以做到这一点,但为什么不简单地:

select *
from contract c
where c.salesid not in (select s.id
                        from salesmen s)

An outer join could indeed do it, but why not simply:

select *
from contract c
where c.salesid not in (select s.id
                        from salesmen s)
指尖上的星空 2024-12-16 20:39:41

我想这就是答案:

select * from Contract c 
right outer join Salesmen s on (c.salesid = s.id)

I suppose this is the answer :

select * from Contract c 
right outer join Salesmen s on (c.salesid = s.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文