id parent_id child_id 1 1 1 2 2 2 3 2 2 4 1 1
我有一个表,当我用id查询时,我需要从数据中获取公共值...例如,如果id = 2且id = 3则返回
id parent_id 2 2 3 2
我经过各种各样的例子狩猎后试过这个:
SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id
FROM user_interest ta
WHERE ta.user_id=2 AND
(SELECT COUNT(*) FROM user_interest tb
WHERE ta.interest_parent_id=tb.interest_parent_id
AND tb.user_id=3 )>1
但它只响应:
id parent_id 2 2
任何帮助:(即时通讯使用带有php / codeigniter的mysql数据库来编写脚本
答案 0 :(得分:1)
你可以尝试一下:
SELECT
tOne.id,
tOne.parent_id
FROM
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tOne
INNER JOIN
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tTwo
ON tOne.parent_id = tTwo.parent_id
AND tOne.id <> tTwo.id
ORDER BY tOne.parent_id;
欢迎任何有关优化查询的建议。
编辑:SQL FIDDLE
答案 1 :(得分:0)
你可以制作一个子SELECT:
SELECT * FROM table WHERE Name IN (SELECT Name FROM table GROUP BY Name HAVING count(*) > 1)