用戶表 table_1id name sex status1 渣渣徽 1 1 2 谷田樂(lè) 1 1用戶角色等級(jí)表 table_2UID 為表一關(guān)聯(lián)字段
id uid level_name level_id1 1 青銅 1 1 1 白銀 21 2 白銀 21 2 黃金 3查詢(xún)所有青銅等級(jí)的用戶,這樣單條件沒(méi)問(wèn)題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但是如何查詢(xún)同時(shí)擁有青銅與白銀的角色呢?如下查詢(xún)條件是不行的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此條件該如何查詢(xún)?
2 回答

翻翻過(guò)去那場(chǎng)雪
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
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'
添加回答
舉報(bào)
0/150
提交
取消