user1019144
user1019144

Reputation: 1253

mysql combine select from 3 tables with one different column

I have 3 similar tables with 1 different column

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

Is there a way to return all combined where user_id = 1 and order by date ?

Upvotes: 0

Views: 29

Answers (1)

quentinadam
quentinadam

Reputation: 3150

I suppose you want something like this:

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

Upvotes: 1

Related Questions