MySql查询返回不需要的结果

时间:2013-03-25 14:11:31

标签: mysql sql

我有一个如下的用户表:

      id                      some attribute
1196670907478919031,fb39f78853bfe42279a8f56bc5671eb9,.....
4915018864030990478,44ecf841a778525b925023321761e458, ........
3161434036923224050,2fd54178a92a4b1a83643d9faa1fec9e,......

现在我有我的朋友表如下

      a                          b               status
 '4915018864030990478', '3161434036923224050', 'accepted'
 '4915018864030990478', '1196670907478919031', 'accepted'

要获取id = 4915018864030990478的用户的所有朋友,我写了以下查询,但我得到以下结果  查询

select id, firstName, lastName, gender, pictureSet  FROM  Member m JOIN  friendedges f ON f.a = m.id where a = "4915018864030990478";

结果

 '4915018864030990478', 'sdsds', 'dsd', 'M', '1'
 '4915018864030990478', 'sdsds', 'dsd', 'M', '1'

我必须得到朋友的ids。我不知道为什么会产生重复的结果。

2 个答案:

答案 0 :(得分:1)

使用DISTINCT删除重复项:

select DISTINCT f.b, firstName, lastName, gender, pictureSet  
FROM  Member m
JOIN  friendedges f ON f.a = m.id 
where f.a = "4915018864030990478";

答案 1 :(得分:0)

你想要的是这个:

select f.id, f.firstName, f.lastName, f.gender, f.pictureSet
FROM Member m
JOIN friendedges e ON e.a = m.id
JOIN Member f ON e.b = f.id
where e.a = "4915018864030990478";

或更简单(如果您的成员不需要数据):

select id, firstName, lastName, gender, pictureSet
FROM  Member m JOIN  friendedges f ON f.b = m.id
where a = "4915018864030990478";

在第二个查询中,我刚将f.a更改为f.b