mysql - How to select data given several rows match? -
i have configuration table , users table.
users:
| id | name | |----|----------| | 0 | bob | | 1 | ted | | 2 | sam |
config:
| user_id | name | value | |---------|----------|-------| | 0 | | 11 | | 0 | b | 2 | | 0 | c | 54 | | 1 | | 5 | | 1 | b | 3 | | 1 | c | 0 | | 2 | | 1 | | 2 | b | 74 | | 2 | c | 54 |
i normalized configuration way since config of unknown amount, have query users based on config, couldn't stored in serialized form.
my issue how find users based on multiple rows? instance: select users > 4 , b < 5
this should return bob , ted.
try this:
select us.name users exists (select name config name='a' , value>4 , user_id=us.id) , exists (select name config name='b' , value<5 , user_id=us.id)
alternatively, can use 2 joins:
select us.name users us, config c1, config c2 us.id=c1.user_id , c1.name='a' , c1.value<4 , us.id=c2.user_id , c2.name='b' , c2.value>5
Comments
Post a Comment