Select Union all 怎样对记录进行区分
2007-06-21 00:00
176 查看
问题:有几个表,要把查询结果给union起来,但是这些表除表名不同外,没有一个字段区分它们。
比如
表1,表2,表3
都有字段id, title ,content
然后
select * from (
select id,title,content from tbl_1 where xxx
union ALL
select id,title,content from tbl_2 where xxx
union ALL
select id,title,content from tbl_3 where xxx
)T
结果是
id title content
1 标题1 内容1
2 标题2 内容2
。。。
问题,怎么样可以做到:
id title content
1 标题1 内容1 --- 属于表2
2 标题2 内容2 --- 属于表1
。。。
答:
select * from (
select id,title,content,'tbl1' as typename from tbl_1 where xxx
union ALL
select id,title,content,'tbl2' as typename from tbl_2 where xxx
union ALL
select id,title,content ,'tbl3' as typename from tbl_3 where xxx
)T
就可以了。
比如
表1,表2,表3
都有字段id, title ,content
然后
select * from (
select id,title,content from tbl_1 where xxx
union ALL
select id,title,content from tbl_2 where xxx
union ALL
select id,title,content from tbl_3 where xxx
)T
结果是
id title content
1 标题1 内容1
2 标题2 内容2
。。。
问题,怎么样可以做到:
id title content
1 标题1 内容1 --- 属于表2
2 标题2 内容2 --- 属于表1
。。。
答:
select * from (
select id,title,content,'tbl1' as typename from tbl_1 where xxx
union ALL
select id,title,content,'tbl2' as typename from tbl_2 where xxx
union ALL
select id,title,content ,'tbl3' as typename from tbl_3 where xxx
)T
就可以了。
相关文章推荐
- mybatis中insert into ...select ...from dual union all select ... from dual 提示sql命令未结束的问题
- [VB.NET]怎样对数据库中的所有记录进行搜索?
- (记录合并)union和union all 的区别
- 对自己的上网搜索记录进行爬虫是怎样一种体验
- select.union all .where.解决方案
- (记录合并)union和union all 的区别
- mysql insert一条记录(事务提交之前)怎样返回创建记录的主键id,last_insert_id(),selectkey && <select> 标签属性
- (记录合并)union和union all 的区别
- 在"Select top 2 * from news order by count"进行筛选时,如果count值有重复时,则筛选结果将不是2条记录,这个问题该怎么解决?
- 使用or展开进行sql优化(即sql语法union all代替or可以提高效率)
- mysql中的联合查询union和union all 学习记录。
- 'union all' two select sentences contain 'order by'
- (记录合并)union和union all 的区别
- union 与union all都是合并结果集,不过它们之间是有区别的,union会自动过滤重复的记录值,union all则不会过滤
- 轨迹记录App是怎样对定位轨迹进行过滤、优化和平滑处理的
- (记录合并)union和union all 的区别
- 怎样进行微信营销才比较好
- MySQL数据库按指定格式导出数据进行备份(select into outfile)和恢复数据的方法
- 怎样对 android 进行优化
- SQL Union和SQL Union All用法 简单举例