How to query the data that Mysql join owns at the same time

user table table_1

id  name  sex status

1    1   1    
2    1   1

user role rating table table_2

UID 

id  uid  level_name  level_id
1     1           1 
1     1           2
1     2           2
1     2           3

query users of all bronze grades, so that there is no problem with single condition
SQL

select * from table_1 RIGHT JOIN table_2 ON table_1.id = table_2.uid
where table_1.status = 1 AND table_2.level_id = 1 group by table_1.id

but how to query roles that have both bronze and silver?
the following query conditions are not allowed

select * from table_1 RIGHT JOIN table_2 ON table_1.id = table_2.uid
where table_1.status = 1 AND table_2.level_id = 1 
AND table_2.level_id = 2

how to query this condition?

Apr.07,2021

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.uid
GROUP  BY uid
HAVING gid = '1,2'

clipboard.png


select * from table_1 RIGHT JOIN table_2 ON table_1.id = table_2.uid
where table_1.status = 1 AND table_2.level_id = 1 AND table_2.level_id = 2

this code means that the record of the same table2 is both level 1 and level 2, which is a false proposition.
if you change it, you need to right join table2 twice

select table_1.* from table_1 
RIGHT JOIN table_2 t2Lv1 ON table_1.id = t2Lv1.uid ON t2Lv1.level_id = 1
RIGHT JOIN table_2 t2Lv2 ON table_1.id = t2Lv2.uid ON t2Lv2.level_id = 2
where table_1.status = 1
GROUP BY table_1.id
This is the general idea of

.


try this query

SELECT
    t.*
FROM
    (
        SELECT
            uid,
            GROUP_CONCAT(level_id) AS level_ids
        FROM
            `user_roles`
        GROUP BY
            uid
    ) AS t
WHERE
    FIND_IN_SET('1', t.level_ids)
AND FIND_IN_SET('2', t.level_ids);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ff40-2c4c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ff40-2c4c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?