我正在尝试完成以下sql语句,但我在响应中得到一个重复的id。
SELECT ci.customer_id,
ci.first_name,
ci.user_gender,
ci.customer_status,
fr.relation
FROM customerinfo ci
INNER JOIN familyrelation fr
ON ( fr.personid_two = ci.customer_id )
WHERE ci.customer_id IN (SELECT personid_two
FROM familyrelation
WHERE personid_one = 17)
AND ci.csp_user_id = 5;
当我运行此查询时,我正在获取正确的结果,但是一个 customer_id 正在重复。任何帮助/建议都非常感谢。
答案 0 :(得分:0)
如果您的数据看起来像这样
drop table if exists ci,fr;
create table ci(customer_id int, name varchar(3),csp_user_id int);
create table fr(personid_one int,personid_two int,relation varchar(10));
insert into ci values (1,'aaa',5),(2,'bbb',5);
insert into fr values (17,1,'mother'),(17,1,'father'),(17,2,'niece');
然后您的查询选择我期望的行
SELECT ci.customer_id,
ci.name,
fr.relation
FROM ci
INNER JOIN fr
ON ( fr.personid_two = ci.customer_id )
WHERE ci.customer_id IN (SELECT personid_two
FROM fr
WHERE personid_one = 17)
AND ci.csp_user_id = 5;
+-------------+------+----------+
| customer_id | name | relation |
+-------------+------+----------+
| 1 | aaa | mother |
| 1 | aaa | father |
| 2 | bbb | niece |
+-------------+------+----------+
3 rows in set (0.00 sec)