mysql组合从3个表中选择一个不同的列

时间:2015-03-11 23:06:26

标签: php mysql

我有3个类似的表,有1个不同的列

id | user_id | post_id | other_id | favorite(tinyint) | date

id | user_id | post_id | other_id | like(tinyint) | date

id | user_id | post_id | other_id | comment(tinyint) | date

有没有办法返回user_id = 1和按日期排序的所有组合?

1 个答案:

答案 0 :(得分:1)

我想你想要这样的东西:

SELECT * FROM (
SELECT id, user_id, post_id, other_id, 'favorite' as type, date FROM table1
UNION 
SELECT id, user_id, post_id, other_id, 'like' as type, date FROM table2
UNION
SELECT id, user_id, post_id, other_id, 'comment' as type, date FROM table3
) AS t ORDER BY date