让所有用户的朋友都有意外的结果

时间:2012-05-24 06:48:55

标签: mysql sql

让我们说:

table users(id, name, ..)
table friends(uid, fid)

如何获得33456位用户ID朋友?

 SELECT fid idd, name
    FROM users, 
         friends 
WHERE uid = 33456
    AND fid = id

但是会给我意想不到的结果

5 个答案:

答案 0 :(得分:2)

SELECT  id, fid, name
FROM users,friends where users.id=friends.fid
WHERE users.id = 33456

答案 1 :(得分:0)

SELECT  id, fid, name
FROM users
INNER JOIN friends ON users.id=friends.fid
WHERE users.id = 33456

答案 2 :(得分:0)

select id,name from users where id in (select fid from friends where uid=33465)

答案 3 :(得分:0)

这将返回所有带有朋友名字的ID。

SELECT b.fid, c.name
FROM users a INNER JOIN friends b ON a.id = b.uid
     INNER JOIN users c on b.fid = c.id
WHERE a.id = 33456

答案 4 :(得分:0)

SELECT u.name,u.id 
FROM users u 
WHERE u.id IN 
(
SELECT f.fid 
FROM users u INNER JOIN friends f ON u.id = f.fid 
WHERE f.uid = 33456 
)