用戶表 table_1id name sex status1 渣渣徽 1 1 2 谷田樂 1 1用戶角色等級表 table_2UID 為表一關聯字段
id uid level_name level_id1 1 青銅 1 1 1 白銀 21 2 白銀 21 2 黃金 3查詢所有青銅等級的用戶,這樣單條件沒問題SQLselect * from table_1 RIGHT JOIN table_2 ON table_1.id = table_2.uidwhere table_1.status = 1 AND table_2.level_id = 1 group by table_1.id但是如何查詢同時擁有青銅與白銀的角色呢?如下查詢條件是不行的select * from table_1 RIGHT JOIN table_2 ON table_1.id = table_2.uidwhere table_1.status = 1 AND table_2.level_id = 1 AND table_2.level_id = 2此條件該如何查詢?
2 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
SELECT LEFT(Group_concat(level_id), Length('1,2')) AS gid, uid, `name`FROM table_1 AS a LEFT JOIN table_2 AS b ON a.id = b.uidGROUP BY uidHAVING gid = '1,2'
添加回答
舉報
0/150
提交
取消