Pao*_*ino 15
除非您有充分的理由这样做,否则您应该将数据标准化并将关系存储在不同的表中.我想也许你在寻找的是:
CREATE TABLE people (
id int not null auto_increment,
name varchar(250) not null,
primary key(id)
);
CREATE TABLE friendships (
id int not null auto_increment,
user_id int not null,
friend_id int not null,
primary key(id)
);
INSERT INTO people (name) VALUES ('Bill'),('Charles'),('Clare');
INSERT INTO friendships (user_id, friend_id) VALUES (1,3), (2,3);
SELECT *
FROM people p
INNER JOIN friendships f
ON f.user_id = p.id
WHERE f.friend_id = 3;
Run Code Online (Sandbox Code Playgroud)
+----+---------+----+---------+-----------+ | id | name | id | user_id | friend_id | +----+---------+----+---------+-----------+ | 1 | Bill | 1 | 1 | 3 | | 2 | Charles | 2 | 2 | 3 | +----+---------+----+---------+-----------+ 2 rows in set (0.00 sec)