如何构建此查询以将id与两个表进行比较?

时间:2012-10-05 07:05:23

标签: php mysql

query logic

我画了这张图片来解释我需要的东西

  

1.将user_id与两个不同表中的user_id进行比较

     

2.然后将两个表中相应的ref_global_id匹配到事件表

     

3.事件表中匹配的global_id然后按升序排列。

3 个答案:

答案 0 :(得分:1)

或者这个:

    SELECT e.global_id, e.event_time
    FROM (SELECT * FROM table1
          UNION
          SELECT * FROM table2) x inner join
          event_table e ON e.global_id = x.ref_global_id
    WHERE x.[user_id] = 121

答案 1 :(得分:0)

SELECT e.global_id, e.event_time
FROM events_table e
JOIN table1 t1 on e.global_id = t1.ref_global_id
JOIN table2 t2 on e.global_id = t2.ref_global_id
WHERE t1.user_id = 121 AND t2.user_id = 121
ORDER BY e.event_time

答案 2 :(得分:0)

试试这个:

select global_id, event_time from event left join table1 on event.global_id = table1.ref_global_id AND table1.user_id = 121 left join table2 on event.global_id = table2.ref_global_id AND table2.user_id = 121