连接两表查询结果的SQL语句

来源:百度知道 编辑:UC知道 时间:2024/06/24 04:29:09
两个表:news1,news2
需要查询的字段:id,title,content,postdate(这些字段的性质两个表都相同,但记录值不同)
我现在需要将两个表里查询的结果结合到一个数据集里,怎么写这样的SQL语句呢?

我现在需要将两个表里查询的结果结合到一个数据集里?
是需要将结果放在一起,并且列的个数不变的话就使用 union(如果允许重复出现相同的记录就是用 union all);
如果是需要将结果拼接起来(列的数目是两个表列数之和)就使用join将两个表拼接起来,
^_^,不同的情况不同的处理,应该可以搞定!

如果只查询两个表有对应关系的资料,则用内连接:
select * from table1 inner join table2 on table1.id=table2.id;
如果查询表1中有的资料,表2中若有则也显示,则使用左连接:
select * from table1 left join table2 on table1.id=table2.id;
不管两个表是否有关联,所有资料都显示,则用外链接:
selecg * from table1 outer join table2 on table1.id=table2.id;

select * from news1
union all
select * from news2

select id,title,content,postdate into new_tablename from new1,new2