gpt4 book ai didi

mysql sql select和inner join for follower/followee names

转载 作者:行者123 更新时间:2023-11-29 07:26:19 25 4
gpt4 key购买 nike

我一直在努力想出一种方法,如何为每个关注者选择所有关注者姓名和关注者姓名。我的表格如下所示

CREATE TABLE person
(
id int(10) auto_increment NOT NULL PRIMARY KEY,
name varchar(100) NOT NULL DEFAULT '',
);

INSERT INTO person (name) VALUES ('John');
INSERT INTO person (name) VALUES ('Alice');
INSERT INTO person (name) VALUES ('Eve');
INSERT INTO person (name) VALUES ('Edgar');
INSERT INTO person (name) VALUES ('Malorie');

CREATE TABLE follows
(
id int(10) NOT NULL DEFAULT '0',
fid int(10) NOT NULL DEFAULT '0'
);

INSERT INTO follows (id,fid) VALUES (1,2);
INSERT INTO follows (id,fid) VALUES (1,3);
INSERT INTO follows (id,fid) VALUES (1,4);
INSERT INTO follows (id,fid) VALUES (2,1);
INSERT INTO follows (id,fid) VALUES (2,5);
INSERT INTO follows (id,fid) VALUES (3,2);
INSERT INTO follows (id,fid) VALUES (5,2);
INSERT INTO follows (id,fid) VALUES (5,1);

到目前为止我已经想出了这样的声明,但显然它没有按需要工作

SELECT person.name FROM person INNER JOIN follows ON (person.id = follows.id) 

我如何进行查询以在一个查询中同时选择 folower 和 folowee 命名他的追随者?

预期的结果应该是这样的

+---------+---------+
| folower | folowee |
+---------+---------+
| John | Eve |
| John | Alice |
| John | Malorie |
| Alice | John |
| Alice | Eve |
| Eve | Alice |
+---------+---------+

最佳答案

您可以对人员使用两次加入,一次用于个人,一次用于追随者姓名

    SELECT person.name , follower.name
FROM person
INNER JOIN follows ON (person.id = follows.id)
INNER JOIN person as follower on follows.fid = follower.id

关于mysql sql select和inner join for follower/followee names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53541696/

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