如何在Mysql中合并和排序两个不同的表

时间:2022-02-22 07:51:35

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:

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:

我希望结果是这样的:是否有任何解决方案允许我将它合并并按照event_date进行排序,如表1所示:

  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

我可以使用while循环或mysql_fetch_assoc来显示内容

2 个解决方案

#1


3  

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


-1  

Maybe you are looking for something like this:

也许你正在寻找这样的东西:

SELECT * FROM tableA FULL OUTER JOIN tableB ORDER BY event_date

通过event_date从表a的完整外部联接表b订单中选择*

#1


3  

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


-1  

Maybe you are looking for something like this:

也许你正在寻找这样的东西:

SELECT * FROM tableA FULL OUTER JOIN tableB ORDER BY event_date

通过event_date从表a的完整外部联接表b订单中选择*