field1 field2
A 1
A 2
A 3
A 4
A 5
B 1
B 3
B 5
C 1
C 2
C 3
我现在想查出field2包含有1,3,5的那些field1,
想得到的结果是:
field1
A
B
查询条件的1,3,5的个数是不定的,如果想要field2包含有1,2的那些field1,
想得到的结果是:
field1
A
C
这样的sql语句应该怎么写?一个sql语句能够实现吗?
12 个解决方案
#1
select distinct(field1)
from Table
where field2 in(1,3,5)
from Table
where field2 in(1,3,5)
#2
declare @s as varchar(10)
set @s = '(1,3,5)'
select distinct(field1)
from Table
where field2 in @s
set @s = '(1,3,5)'
select distinct(field1)
from Table
where field2 in @s
#3
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
#4
查询条件是给出字符串‘1,3,5’呢?
还是给出 1 3 5 三个数字?
还是给出 1 3 5 三个数字?
#5
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('+@s+')-len(replace('+@s+','','',''''))+1
')
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('+@s+')-len(replace('+@s+','','',''''))+1
')
#6
四楼正确
#7
更正:
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('''+@s+''')-len(replace('''+@s+''','','',''''))+1
')
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('''+@s+''')-len(replace('''+@s+''','','',''''))+1
')
#8
谢谢大家!!!
冒牌主人的这个可以:
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
扩张一下>>>--------------------------------------
Table有三个字段,
field1 field2 field3
A 1 12
A 2 6
A 3 12
A 4 12
A 5 12
B 1 6
B 3 12
B 5 6
C 1 12
C 2 12
C 3 12
我现在想查出field2包含有1,3,5,并且field2:1对应的field3>6,field2:3对应的field3>8,field2:5对应的field3>10,
的那些field1,
想得到的结果是:
field1
A
如果想要field2包含有1,2,并且field2:1对应的field3>10,field2:2对应的field3>10,
的那些field1,
想得到的结果是:
field1
C
这样的sql语句应该怎么写?比刚才多了一个字段的判断。。
冒牌主人的这个可以:
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
扩张一下>>>--------------------------------------
Table有三个字段,
field1 field2 field3
A 1 12
A 2 6
A 3 12
A 4 12
A 5 12
B 1 6
B 3 12
B 5 6
C 1 12
C 2 12
C 3 12
我现在想查出field2包含有1,3,5,并且field2:1对应的field3>6,field2:3对应的field3>8,field2:5对应的field3>10,
的那些field1,
想得到的结果是:
field1
A
如果想要field2包含有1,2,并且field2:1对应的field3>10,field2:2对应的field3>10,
的那些field1,
想得到的结果是:
field1
C
这样的sql语句应该怎么写?比刚才多了一个字段的判断。。
#9
create table T(field1 char(1), field2 int, field3 int)
insert T select 'A', 1, 12
union all select 'A', 2, 6
union all select 'A', 3, 12
union all select 'A', 4, 12
union all select 'A', 5, 12
union all select 'B' , 1, 6
union all select 'B' , 3, 12
union all select 'B' , 5, 6
union all select 'C' , 1, 12
union all select 'C' , 2, 12
union all select 'C' , 3, 12
--1
select field1
from T
where field2 in (1,3,5)
and field3>(case field2 when 1 then 6 when 3 then 8 when 5 then 10 end)
group by field1
having count(distinct field2)=3
--result
field1
------
A
(1 row(s) affected)
insert T select 'A', 1, 12
union all select 'A', 2, 6
union all select 'A', 3, 12
union all select 'A', 4, 12
union all select 'A', 5, 12
union all select 'B' , 1, 6
union all select 'B' , 3, 12
union all select 'B' , 5, 6
union all select 'C' , 1, 12
union all select 'C' , 2, 12
union all select 'C' , 3, 12
--1
select field1
from T
where field2 in (1,3,5)
and field3>(case field2 when 1 then 6 when 3 then 8 when 5 then 10 end)
group by field1
having count(distinct field2)=3
--result
field1
------
A
(1 row(s) affected)
#10
--2
select field1
from T
where field2 in (1,2)
and field3>10
group by field1
having count(distinct field2)=2
--result
field1
------
C
(1 row(s) affected)
select field1
from T
where field2 in (1,2)
and field3>10
group by field1
having count(distinct field2)=2
--result
field1
------
C
(1 row(s) affected)
#11
Charindex就是了!!
不过要注意:
比如说传进来的是: 董事长,总经理,经理,课长,组长
如果简单的使用Charindex 就会在总经理和经理 这个地方弄混
所以要这样
where charindex(','+职务+',' , ','+条件字符串+','
不过要注意:
比如说传进来的是: 董事长,总经理,经理,课长,组长
如果简单的使用Charindex 就会在总经理和经理 这个地方弄混
所以要这样
where charindex(','+职务+',' , ','+条件字符串+','
#12
多谢各位!
#1
select distinct(field1)
from Table
where field2 in(1,3,5)
from Table
where field2 in(1,3,5)
#2
declare @s as varchar(10)
set @s = '(1,3,5)'
select distinct(field1)
from Table
where field2 in @s
set @s = '(1,3,5)'
select distinct(field1)
from Table
where field2 in @s
#3
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
#4
查询条件是给出字符串‘1,3,5’呢?
还是给出 1 3 5 三个数字?
还是给出 1 3 5 三个数字?
#5
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('+@s+')-len(replace('+@s+','','',''''))+1
')
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('+@s+')-len(replace('+@s+','','',''''))+1
')
#6
四楼正确
#7
更正:
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('''+@s+''')-len(replace('''+@s+''','','',''''))+1
')
declare @s as varchar(10)
set @s = '1,3,5'
exec('
select field1
from tablename
where field2 in ('+@s+')
group by field1
having count(distinct field2)=len('''+@s+''')-len(replace('''+@s+''','','',''''))+1
')
#8
谢谢大家!!!
冒牌主人的这个可以:
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
扩张一下>>>--------------------------------------
Table有三个字段,
field1 field2 field3
A 1 12
A 2 6
A 3 12
A 4 12
A 5 12
B 1 6
B 3 12
B 5 6
C 1 12
C 2 12
C 3 12
我现在想查出field2包含有1,3,5,并且field2:1对应的field3>6,field2:3对应的field3>8,field2:5对应的field3>10,
的那些field1,
想得到的结果是:
field1
A
如果想要field2包含有1,2,并且field2:1对应的field3>10,field2:2对应的field3>10,
的那些field1,
想得到的结果是:
field1
C
这样的sql语句应该怎么写?比刚才多了一个字段的判断。。
冒牌主人的这个可以:
select field1
from tablename
where field2 in (1,3,5)
group by field1
having count(distinct field2)=3
扩张一下>>>--------------------------------------
Table有三个字段,
field1 field2 field3
A 1 12
A 2 6
A 3 12
A 4 12
A 5 12
B 1 6
B 3 12
B 5 6
C 1 12
C 2 12
C 3 12
我现在想查出field2包含有1,3,5,并且field2:1对应的field3>6,field2:3对应的field3>8,field2:5对应的field3>10,
的那些field1,
想得到的结果是:
field1
A
如果想要field2包含有1,2,并且field2:1对应的field3>10,field2:2对应的field3>10,
的那些field1,
想得到的结果是:
field1
C
这样的sql语句应该怎么写?比刚才多了一个字段的判断。。
#9
create table T(field1 char(1), field2 int, field3 int)
insert T select 'A', 1, 12
union all select 'A', 2, 6
union all select 'A', 3, 12
union all select 'A', 4, 12
union all select 'A', 5, 12
union all select 'B' , 1, 6
union all select 'B' , 3, 12
union all select 'B' , 5, 6
union all select 'C' , 1, 12
union all select 'C' , 2, 12
union all select 'C' , 3, 12
--1
select field1
from T
where field2 in (1,3,5)
and field3>(case field2 when 1 then 6 when 3 then 8 when 5 then 10 end)
group by field1
having count(distinct field2)=3
--result
field1
------
A
(1 row(s) affected)
insert T select 'A', 1, 12
union all select 'A', 2, 6
union all select 'A', 3, 12
union all select 'A', 4, 12
union all select 'A', 5, 12
union all select 'B' , 1, 6
union all select 'B' , 3, 12
union all select 'B' , 5, 6
union all select 'C' , 1, 12
union all select 'C' , 2, 12
union all select 'C' , 3, 12
--1
select field1
from T
where field2 in (1,3,5)
and field3>(case field2 when 1 then 6 when 3 then 8 when 5 then 10 end)
group by field1
having count(distinct field2)=3
--result
field1
------
A
(1 row(s) affected)
#10
--2
select field1
from T
where field2 in (1,2)
and field3>10
group by field1
having count(distinct field2)=2
--result
field1
------
C
(1 row(s) affected)
select field1
from T
where field2 in (1,2)
and field3>10
group by field1
having count(distinct field2)=2
--result
field1
------
C
(1 row(s) affected)
#11
Charindex就是了!!
不过要注意:
比如说传进来的是: 董事长,总经理,经理,课长,组长
如果简单的使用Charindex 就会在总经理和经理 这个地方弄混
所以要这样
where charindex(','+职务+',' , ','+条件字符串+','
不过要注意:
比如说传进来的是: 董事长,总经理,经理,课长,组长
如果简单的使用Charindex 就会在总经理和经理 这个地方弄混
所以要这样
where charindex(','+职务+',' , ','+条件字符串+','
#12
多谢各位!