SQL多个表实现联合查询
2013-11-22 14:12
169 查看
select LineId,Id,Country from Domestic
union all
select LineId,Id,Country from Freedom
-- 联合查询Domestic,Freedom表的LineId,Id,Country all代表不去除反复
--功能:[SQL语句] UNION [SQL语句]将两个语句中选择的同一列中的不同的值筛选出来
SELECT<表1>.<列名> ,<表2><列名>FROM<表1>OUTER JOIN<表2> ON<表1>.<列>=表2>.<列名>
--功能:实现两个表的外连接
Select Domestic.LineId,Freedom.LineId from Domestic,Freedom where Domestic.Sames=Freedom.Sames
Select Domestic.LineId,Freedom.LineId FROM Domestic inner join Freedom on Freedom.Sames=Domestic.Sames
--功能:实现两个表的内连接 把Domestic,Freedom两个表用Domestic.Sames=Freedom.Sames关联起来显示Domestic.LineId,Freedom.LineId
------------------------
我的数据库表是这种:table0101,table0102,table0103,.......各个表有同样的结构,我想用sql语句从查询分析器里导出来,有沒有办法能够一次导出,语句要返回一个结果集.
用union all就能够实现:
select * from table0101
union all
select * from table0102
union all
select * from table0103
union all
select * from table0104
....
****************
补充:假设想去掉反复记录的话,把union all 改成 union就能够了
---
以上,希望对你有所帮助。
union all
select LineId,Id,Country from Freedom
-- 联合查询Domestic,Freedom表的LineId,Id,Country all代表不去除反复
--功能:[SQL语句] UNION [SQL语句]将两个语句中选择的同一列中的不同的值筛选出来
SELECT<表1>.<列名> ,<表2><列名>FROM<表1>OUTER JOIN<表2> ON<表1>.<列>=表2>.<列名>
--功能:实现两个表的外连接
Select Domestic.LineId,Freedom.LineId from Domestic,Freedom where Domestic.Sames=Freedom.Sames
Select Domestic.LineId,Freedom.LineId FROM Domestic inner join Freedom on Freedom.Sames=Domestic.Sames
--功能:实现两个表的内连接 把Domestic,Freedom两个表用Domestic.Sames=Freedom.Sames关联起来显示Domestic.LineId,Freedom.LineId
------------------------
我的数据库表是这种:table0101,table0102,table0103,.......各个表有同样的结构,我想用sql语句从查询分析器里导出来,有沒有办法能够一次导出,语句要返回一个结果集.
用union all就能够实现:
select * from table0101
union all
select * from table0102
union all
select * from table0103
union all
select * from table0104
....
****************
补充:假设想去掉反复记录的话,把union all 改成 union就能够了
---
以上,希望对你有所帮助。
相关文章推荐
- Hibernate Dao中直接使用sql实现复杂的多表联合查询
- sql分组后二次汇总(处理表重复记录查询和删除)的实现方法
- SQL 多表连接查询实现语句
- 解决Mysql数据库实现多表联合查询及按照其中一个字段进行排序问题
- sql left join 联合查询示例
- 深入sql多表差异化联合查询的问题详解
- 全Sql语句实现SBO事务日志记录与查询
- mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL
- oracle:两条sql语句实现层次查询的反序权值计算与输出
- SQL查询,2个表,第一个表一行多个标题,第二个表1列,联合查询变成一个表,1行多个标题变成多个列
- SQL 多表联合查询
- sql语句实现查询实例
- Sql连接查询和联合查询
- 数据库insert and update--查询是否主键存在和插入语句用一条sql来实现。
- SQL联合查询中的关键语法
- ASP.NET实现列表页连接查询 拼接sql语句 绑定grivdView
- SQL数据库查询实现行转列与列转行结果SQL语句(适用于SqlServer数据库,oracle需要修改case when语句)
- Oracle实现分页查询的SQL语法汇总
- 全球征集-如何实现回文SQL的查询
- SQL语句实现查询SQL Server服务器名称和IP地址