我有如下的会员表
MemberID Name BookID
1 ABC 10
1 ABC 14
2 XYZ 10
3 PQR 14
我想选择一个包含单行BOOKID 10和14的MemberID.
预期产出:
MemberID
1
我试过下面的代码,但它不起作用:
select MemberID from member where BookID IN (10,14)
最佳答案 你可以尝试这样:
SELECT MemberID, GROUP_CONCAT(BookID) AS BookID
FROM member
GROUP BY MemberID
HAVING FIND_IN_SET(10, BookID) > 0 AND FIND_IN_SET(14, BookID) > 0
这是SQLFIDDLE
.
另一个解决方案是使用JOIN,如下所示:
SELECT x.MemberID
FROM member x
INNER JOIN member y ON x.MemberID = y.MemberID
AND x.BookID = 10
AND y.BookID = 14
这是SQLFIDDLE
.