I have member table like below
MemberID Name BookID
1 ABC 10
1 ABC 14
2 XYZ 10
3 PQR 14
I want to select a MemberID
that contains both the BOOKID 10 and 14
in single row.
Expected Output:
MemberID
1
I have tried below code but it doesnt work:
select MemberID from member where BookID IN (10,14)
You can try like this:
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
Here is the SQLFIDDLE
.
Another solution can be using JOIN
like this:
SELECT x.MemberID
FROM member x
INNER JOIN member y ON x.MemberID = y.MemberID
AND x.BookID = 10
AND y.BookID = 14
Here is the SQLFIDDLE
.
您可以使用DISTINCT
关键字
select distinct MemberID from member where BookID
What you will want to do is select distinct MemberID. Something like
SELECT DISTINCT MemberID FROM member
WHERE BookID IN (10,14)
Assuming you want results for books with Id 10 or 12.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.