问题
I have table A:
id username firstname event_date
1 Ben lori 2014-01-27
2 Ben lori 2014-01-04
3 Mary john 2014-01-28
I have table B:
id username event_date
1 Ben 2014-01-23
2 Nicole 2014-01-26
I want the result to be like this: Is there any solution that allow me to combine and sort it according to event_date like one table,like this:
2 Ben lori 2014-01-04
1 Ben 2014-01-23
2 Nicole 2014-01-26
1 Ben lori 2014-01-27
3 Mary john 2014-01-28
so that I can use something like while loop or mysql_fetch_assoc to display the content
回答1:
Basically, you need union all
:
select id, username, firstname, event_date
from ((select id, username, firstname, event_date
from tablea
) union all
(select id, username, NULL as firstname, event_date
from tablea
)
) t
order by event_date;
回答2:
Maybe you are looking for something like this:
SELECT *
FROM tableA FULL OUTER JOIN tableB
ORDER BY event_date
来源:https://stackoverflow.com/questions/21493530/how-to-combine-and-sort-two-different-tables-together-in-mysql