合并两个表查询结果
2011-07-08 16:08
627 查看
1.两个不同的表,现在要查询这两个表中的Id,并合并查询结果:(去年重复列)
select brandId from acc_brand union select categoryId from ACC_category
2.两个不同的表,现在要查询这两个表中的Id,并合并查询结果(保留重复列):
select brandId from acc_brand union all select categoryId from ACC_category
3.两个不同的表,现在要查询这两个表中的Id,并合并查询结果,结果中要显示出值是哪个表的:
select 'acc' as tablename, brandId from acc_brand union select 'cate' ,categoryId from ACC_category
4.两个不同的表,现在要查询这两个表中的Id,并合并查询结果, 查询结果去年Id重复的值 :
select * from (select *,row_number() over(partition by brandId order by brandId) as rowIndex from
(select 'acc' as tablename, brandId from acc_brand union select 'cate' ,categoryId from ACC_category) as result) as a
where a.rowindex=1
注:row_number() over(partition by 列名 order by 列名) 对分组结果进行排序
select brandId from acc_brand union select categoryId from ACC_category
2.两个不同的表,现在要查询这两个表中的Id,并合并查询结果(保留重复列):
select brandId from acc_brand union all select categoryId from ACC_category
3.两个不同的表,现在要查询这两个表中的Id,并合并查询结果,结果中要显示出值是哪个表的:
select 'acc' as tablename, brandId from acc_brand union select 'cate' ,categoryId from ACC_category
4.两个不同的表,现在要查询这两个表中的Id,并合并查询结果, 查询结果去年Id重复的值 :
select * from (select *,row_number() over(partition by brandId order by brandId) as rowIndex from
(select 'acc' as tablename, brandId from acc_brand union select 'cate' ,categoryId from ACC_category) as result) as a
where a.rowindex=1
注:row_number() over(partition by 列名 order by 列名) 对分组结果进行排序
相关文章推荐
- mysql 合并两个查询结果
- 按行合并两个sql的查询结果
- (转载)按行合并两个sql的查询结果
- mysql将查询结果中字段合并为字符串group_concat函数
- mysql查询结果合并
- oracle数据库将多个列的查询结果集合并到一行中
- HQL多表联合查询,两个表的元素怎样组合并压入一个对象中
- SQL 字段charindex,stuff,ltrim, for xml path('') 操作字符串 合并查询结果项值
- mysql如何实现多行查询结果合并成一行
- 【代码笔记】【MySQL】 合并两个结果集
- mysql多表结果合并查询
- mysql中的多行查询结果合并成一个
- oracle 多个查询语句结果合并
- mysql数据库中把查询的结果列合并(GROUP_CONCAT)
- 把查询结果的两行的同一列合并的方法
- 合并union 和union all用法-sql语句查询结果
- 将同个字段查询结果合并
- union(联合)合并查询结果
- mySql联合两个查询结果用UNION或者UNION ALL