select distinct 字段1 from 表 union select distinct 字段2 from 表
create table #T( l1 char(1) ,l2 char(1)) insert into #T select 'a','x' insert into #T select 'b','y' insert into #T select 'a','y' insert into #T select 'a','x' insert into #T select 'b','z' insert into #T select 'c','y' select l1 from #T union select l2 from #T /* l1 ---- a b c x y z(所影响的行数为 6 行) */
--借用下数据create table #T( l1 char(1) ,l2 char(1)) insert into #T select 'a','x' insert into #T select 'b','y' insert into #T select 'a','y' insert into #T select 'a','x' insert into #T select 'b','z' insert into #T select 'c','y' select distinct l1 from #T union all select distinct l2 from #T
select distinct 字段1 from 表 union select distinct 字段2 from 表
谢谢各位,还有个问题,问完结贴 要加限制条件怎么加? select distinct 字段1 from 表 union select distinct 字段2 from 表 where 字段3='XXX'这样加查不到正确答案,还望教我!!!!!1 a x 1 b y 1 a y 1 a x 1 b z 1 c y 2 a x 2 b y 2 a y 2 a x 2 b z 2 c y
第一个问题。直接union就可以了,本身就是去重复的。第二个问题。select * from (select 字段2 from tb union select 字段3 from tb ) a where 字段2='xxx'
1 a x 1 b y 1 a y 1 a x 1 b z 1 c y 2 e 11 2 f 22 2 e 22 2 e 11 2 f 33 2 g 22输出: e f g 11 22 33
比如我 条件为:字段1=1 得到: a b c x y z条件为:字段1=2 得到: e f g 11 22 33
select 字段2 from tb where 字段1='1' union select 字段3 from tb where 字段1='1'--或者select 字段2 from ( select 字段1,字段2 from tb union select 字段1,字段3 from tb ) x where 字段1='1'
Select cola from ta where colb='1' union select cola as cola from tb where colb='2'
create table #T( l1 char(1) ,l2 char(1))
insert into #T select 'a','x'
insert into #T select 'b','y'
insert into #T select 'a','y'
insert into #T select 'a','x'
insert into #T select 'b','z'
insert into #T select 'c','y'
select l1 from #T
union
select l2 from #T
/*
l1
----
a
b
c
x
y
z(所影响的行数为 6 行)
*/
--借用下数据create table #T( l1 char(1) ,l2 char(1))
insert into #T select 'a','x'
insert into #T select 'b','y'
insert into #T select 'a','y'
insert into #T select 'a','x'
insert into #T select 'b','z'
insert into #T select 'c','y'
select distinct l1 from #T
union all
select distinct l2 from #T
union会删除重复数据,union all不会,他会完整的显示全部的数据
要加限制条件怎么加?
select distinct 字段1 from 表 union select distinct 字段2 from 表 where 字段3='XXX'这样加查不到正确答案,还望教我!!!!!1 a x
1 b y
1 a y
1 a x
1 b z
1 c y
2 a x
2 b y
2 a y
2 a x
2 b z
2 c y
(select 字段2 from tb
union
select 字段3 from tb
) a
where 字段2='xxx'
1 b y
1 a y
1 a x
1 b z
1 c y
2 e 11
2 f 22
2 e 22
2 e 11
2 f 33
2 g 22输出:
e
f
g
11
22
33
比如我
条件为:字段1=1
得到:
a
b
c
x
y
z条件为:字段1=2
得到:
e
f
g
11
22
33
union
select 字段3 from tb where 字段1='1'--或者select 字段2
from
(
select 字段1,字段2 from tb
union
select 字段1,字段3 from tb
) x
where 字段1='1'
union
select cola as cola from tb where colb='2'