MySQL 组合从 3 个表和一个不同的列中进行选择


mysql combine select from 3 tables with one different column

我有 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 并按日期排序?

我想

你想要这样的东西:

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