data from two tables and one show

一笑奈何 提交于 2020-01-06 08:48:12

问题


I have two similar table in database, for example:

news1:

id | title | body|
1  | aaa   | aaa |
2  | ggg   | bbb |
2  | xxx   | ccc |

and news2:

id | title | body | photo |
1  | BBB   | 111  | 111
2  | RRR   | 222  | 222
3  | EEE   | 333  | 333

how can i get data from two tables and show in template for example order by title?

title | body | photo
aaa   | aaa
BBB   | 111  | 111
EEE   | 333  | 333
ggg   | ggg
RRR   | 222  |222
xxx   | xxx

?


回答1:


use Union

SELECT *
  FROM news1 n1
UNION ALL
SELECT *
  FROM news2 n2

UNION ALL will be faster, but won't remove duplicates if they exist. Use UNION if you want duplicates removed. Joining two similar tables in MySQL



来源:https://stackoverflow.com/questions/6965565/data-from-two-tables-and-one-show

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