how to combine and sort two different tables together in Mysql

谁都会走 提交于 2020-01-15 23:43:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!