gpt4 book ai didi

Mysql:帮助从两个表进行查询

转载 作者:行者123 更新时间:2023-11-30 00:15:01 26 4
gpt4 key购买 nike

我有两个表:用户、 friend ,其结构如下:

用户:

+------------+--------------+.  
| users_id | username |.
+------------+--------------+.
| 1 | Mark |.
| 2 | Alex |.
| 3 | Anna |.
+------------+--------------+.

friend :

+------------+--------------+.  
| id |fromid | toid |.
+------------+--------------+.
| 1 | 2 | 3 |.
| 2 | 1 | 2 |.
| 3 | 1 | 3 |.
+------------+--------------+.

检索这些结果的查询是什么:

+------------+--------------+.  
| from | to |
+------------+--------------+.
| Alex | Anna |
| Mark | Alex |.
| Mark | Anna |.
+------------+--------------+.

P.s.:如果我的问题中的表格格式不正确,请原谅

最佳答案

SELECT u1.name as from_name,
u2.name as to_name
FROM Friends AS f
JOIN Users AS u1 ON u1.users_id = f.fromid
JOIN Users AS u2 ON u2.users_id = f.toid

这样就可以了。

关于Mysql:帮助从两个表进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23726003/

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