SQL 连接内部或外部

发布于 2024-12-07 06:09:21 字数 616 浏览 8 评论 0原文

我有两张桌子: 用户:id 用户名 部落 部落: id clan crest

我尝试按用户名进行搜索,并输出用户名的部落。

查询:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` JOIN `clans` ON `users`.`username` LIKE '%gu%' 

输出:

id  username    clan       crest       
2   gusar                  26_760.gif 
3   gusar11     Tong       26_760.gif 

如果部落为空,波峰将为空..但是...(

我想要以下结果:

id  username    clan       crest       
2   gusar                  
3   gusar11     Tong       26_760.gif 

I have two tables:
users: id username clan
clans: id clan crest

I trying to do the search by username with output the clan of username.

Query:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` JOIN `clans` ON `users`.`username` LIKE '%gu%' 

Output:

id  username    clan       crest       
2   gusar                  26_760.gif 
3   gusar11     Tong       26_760.gif 

If clan is empty crest will be empty.. But ... (

I want following result:

id  username    clan       crest       
2   gusar                  
3   gusar11     Tong       26_760.gif 

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

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

发布评论

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

评论(4

郁金香雨 2024-12-14 06:09:21

您使用 LIKE 从一张表连接到另一张表,通常没有什么理由这样做。相反,您可能希望在 WHERE 子句中使用它,并且应该使用外键在表之间进行连接。

您想要使用 LEFT OUTER JOIN(或简称 LEFT JOIN)来尝试匹配第二个表中的所有行,但如果没有匹配则显示 NULL 值:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest`
FROM `users` 
LEFT JOIN `clans` ON `users`.`clan` = `clans`.`clan`
WHERE `users`.`username` LIKE '%gu%'

请注意,在 users 表中,理想情况下您应该保留部落 ID,而不是部落头衔。然后,您可以使用此查询来执行搜索:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest`
FROM `users` 
LEFT JOIN `clans` ON `users`.`clanid` = `clans`.`id`
WHERE `users`.`username` LIKE '%gu%'

You're joining from one table to another using a LIKE, typically there is little reason why you would do this. Instead you likely want that in a WHERE clause, and you should be joining between the table using a foreign key.

You want to use a LEFT OUTER JOIN (or LEFT JOIN for short) to attempt to match all of the rows in the second table, but to show NULL values if there was no match:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest`
FROM `users` 
LEFT JOIN `clans` ON `users`.`clan` = `clans`.`clan`
WHERE `users`.`username` LIKE '%gu%'

Mind you, in your users table, ideally you should hold the clan id and not the clan title. You would then use this query to perform your search:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest`
FROM `users` 
LEFT JOIN `clans` ON `users`.`clanid` = `clans`.`id`
WHERE `users`.`username` LIKE '%gu%'
忆沫 2024-12-14 06:09:21

如果您想要第一个表的结果,即使第二个表中没有匹配项,您也需要使用 LEFT OUTER JOIN。

If you want results from the first table, even when there are NO matches in the second table, you want to use a LEFT OUTER JOIN.

不交电费瞎发啥光 2024-12-14 06:09:21

尝试

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` LEFT JOIN `clans` ON `users`.`username` LIKE '%gu%' 

Try

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` LEFT JOIN `clans` ON `users`.`username` LIKE '%gu%' 
誰認得朕 2024-12-14 06:09:21

您需要使用某些条件连接表。您正在使用 ON,而您应该使用 WHERE。我假设users.clanclans.clans相关?试试这个:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` LEFT JOIN `clans` ON `users`.`clan` = `clans`.`clan` WHERE `users`.`username` LIKE '%gu%'

You need to join the table using some condition. You're using an ON where you should use a WHERE. I assume users.clan relates to clans.clans? Try this:

SELECT `users`.`id`, `users`.`username`,`users`.`clan`, `clans`.`crest` FROM `users` LEFT JOIN `clans` ON `users`.`clan` = `clans`.`clan` WHERE `users`.`username` LIKE '%gu%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文