gpt4 book ai didi

mysql - 如何在 mysql 查询中使用不存在或不存在的内部连接?

转载 作者:行者123 更新时间:2023-11-29 01:35:33 24 4
gpt4 key购买 nike

我有这样的数据对于用户

id | username | email
1 | test | test@test.com
2 | om | test2@test2.com
3 | aa | test3@test3.com

我有这样的数据用户生物数据

id | username | bio
1 | test | test
2 | om | test2

所以,我想使用 not in with inner join 来显示数据 where user.username not in biodata.username 并且我尝试这样但是它是错误的

select user.*, biodata.* from user inner join biodata on user.username = biodata.username where user.username not in biodata.username;

那么,如何使用它呢?

最佳答案

这需要常见的 LEFT JOIN ... IS NULL 模式。

SELECT u.id, u.username, u.email
FROM user u
LEFT JOIN biodata b ON u.username = b.username
WHERE b.id IS NULL

LEFT JOIN 操作会保留 user 中的所有行,无论它们在 biodata 中是否匹配。 (相比之下,普通的 JOIN 会抑制来自 user 的没有匹配项的行。) b.id IS NULL 操作过滤掉确实匹配的行。

关于mysql - 如何在 mysql 查询中使用不存在或不存在的内部连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com