简体   繁体   中英

MySQL: Select several rows based on several keys from two different tables

I have these two tables - user_schedules and user_schedule_meta , shown below:

------------------------------------
| id | scheduler_id | status | 
------------------------------------
  1        3          pending 
  2        5          active
  3        6          active

and

----------------------------------------------
| id | user_schedule_id | meta_key |meta_value
----------------------------------------------
 1         3              course-id    135
 2         3              session-id   15
 3         3              schedule-id  120

I want to write a query to enable me select, for example, from both tables where EVERYONE of the below 5 conditions are met:

  1. user_schedule_id = 3
  2. scheduler_id = 6
  3. session_id = 15
  4. course-id = 135
  5. schedule-id = 120

This is what I have so far, but it is not working:

SELECT user_schedule_meta.`id` FROM user_schedule_meta, user_schedules 
WHERE user_schedules.`scheduler_id` = 6 
AND user_schedules.id = user_schedule_meta.`user_schedule_id` 
AND ( 
(user_schedule_meta.`meta_key` = 'course-id' AND user_schedule_meta.`meta_value` = 135) 
OR (user_schedule_meta.`meta_key` = 'session-id' AND user_schedule_meta.`meta_value` = 15) 
OR (user_schedule_meta.`meta_key` = 'daily-schedule-id' AND user_schedule_meta.`meta_value` = 120) 
) 
GROUP BY user_schedule_meta.`id`

Any suggestions what I am not doing right?

This is a typical key-value store lookup problem. These are trickier than they look in SQL, in that they require multiple JOIN operations.

You need a virtual table with one row per user_schedules.id value, then you can filter it. So

SELECT u.id, u.scheduler_id
  FROM user_schedules u
  JOIN user_schedule_meta a ON u.id=a.user_schedule_id AND a.meta_key='course-id'
  JOIN user_schedule_meta b ON u.id=b.user_schedule_id AND b.meta_key='session-id'
  JOIN user_schedule_meta c ON u.id=c.user_schedule_id AND c.meta_key='daily-schedule-id'

WHERE a.meta_value = 135  -- value associated with course-id
  AND b.meta_value=15     -- value associated with session-id
  AND c.meta_value=120    -- value associated with daily-schedule-id

Notice also that you can list your table with associated attributes like this. This trick of joining the key/value table multiple times is a kind of pivot operation. I use LEFT JOIN because it will allow the result set to show rows where an attribute is missing.

SELECT u.id, u.scheduler_id, u.status,
       a.meta_value AS course_id,
       b.meta_value AS session_id,
       c.meta_value AS daily_schedule_id
  FROM user_schedules u
  LEFT JOIN user_schedule_meta a ON u.id=a.user_schedule_id AND a.meta_key='course-id'
  LEFT JOIN user_schedule_meta b ON u.id=b.user_schedule_id AND b.meta_key='session-id'
  LEFT JOIN user_schedule_meta c ON u.id=c.user_schedule_id AND c.meta_key='daily-schedule-id'

try this is code

select * from user_schedule_meta where user_schedule_id=3 and 
(meta_key='session-id' AND meta_value=15
or meta_key='daily-schedule-id' AND meta_value=120
or meta_key='course-id' AND meta_value=135
)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM