我使用的是SQL Server 2008 R2。
我有一个奇怪的问题如下。我有一张表,如
所示
我需要编写如下查询:
SELECT DISTINCT Field1
FROM MYTABLE
WHERE Field2 IN (96,102)
在此查询中,WHERE Field2 IN (96,102)
给了我96或102或两者!
更重要的是,我想同时返回包含96和102的行!
有什么建议吗?请写出面向结果......
答案 0 :(得分:2)
我为此做了sqlfiddle ..
create table a (id int, val int)
go
insert into a select 1, 22
insert into a select 1, 122
insert into a select 2, 22
insert into a select 3, 122
insert into a select 4, 22
insert into a select 4, 122
然后像这样选择
select count(distinct id), id
from a
where val in (22, 122)
group by id
having count(id) > 1
编辑:count(非常ID)只会显示不同的计数..
答案 1 :(得分:1)
编辑:
这是一个sqlfiddle示例(感谢Mark Kremers):
http://sqlfiddle.com/#!3/df201/1
create table mytable (field1 int, field2 int)
go
insert into mytable values (199201, 84)
insert into mytable values (199201, 96)
insert into mytable values (199201, 102)
insert into mytable values (199201, 103)
insert into mytable values (581424, 96)
insert into mytable values (581424, 84)
insert into mytable values (581424, 106)
insert into mytable values (581424, 122)
insert into mytable values (687368, 79)
insert into mytable values (687368, 96)
insert into mytable values (687368, 102)
insert into mytable values (687368, 104)
insert into mytable values (687368, 106)
以下是查询:
select distinct a.field1 from
( select field1 from mytable where field2=96) a
inner join
( select field1 from mytable where field2=102) b
on a.field1 = b.field1
以下是结果:
FIELD1
199201
687368
最后,这是查询的简化版本(thans to pst):
select distinct a.field1 from mytable a
inner join mytable b
on a.field1 = b.field1
where a.field2=96 and b.field2=102
答案 2 :(得分:1)
使用自我加入?不是最整洁,但我认为它适用于2个值
SELECT *
FROM T R1
JOIN T R2 -- join table with itself
ON R1.F1 = R2.F1 -- where the first field is the same
WHERE R1.F2 = 96 AND R2.F2 = 102 -- and each has one of the required values
(T =表,Rx =关系别名,Fx =场)
如果可以有任意数量的字段,这可以解决为
CREATE TABLE #T (id int, val int)
GO
INSERT INTO #T (id, val)
VALUES
(1, 22), (1, 22), -- no, only 22 (but 2 records)
(2, 22), (2, 122), -- yes, both values (only)
(3, 122), -- no, only 122
(4, 22), (4,122), -- yes, both values ..
(4, 444), (4, null), -- and extra values
(5, 555) -- no, neither value
GO
-- Using DISTINCT over filtered results first, as
-- SQL Server 2008 does not support HAVING COUNT(DISTINCT F1, F2)
SELECT id
FROM (SELECT DISTINCT id, val
FROM #T
WHERE val IN (22, 122)) AS R1
GROUP BY id
HAVING COUNT(id) >= 2 -- or 3 or ..
GO
-- Or a similar variation, as can COUNT(DISTINCT ..)
-- in the SELECT of a GROUP BY
SELECT id
FROM (SELECT id, COUNT(DISTINCT val) as ct
FROM #T
WHERE val IN (22, 122)
GROUP BY id) AS R1
WHERE ct >= 2 -- or 3 or ..
GO
对于较大的IN (..)
尺寸,例如超过20个值,可能建议使用单独的表格或表格值以及JOIN
出于性能原因。
答案 3 :(得分:0)
尝试使用原始查询:
SELECT DISTINCT Field1
FROM MYTABLE
WHERE rtrim(ltrim(cast(Field2 as varchar))) IN ('96','102')
答案 4 :(得分:-1)
与我合作
create table a (id int, val int)
insert into a select 1, 22
insert into a select 1, 122
insert into a select 2, 22
insert into a select 3, 122
insert into a select 4, 22
insert into a select 4, 122
create table b (id int)
insert into b values (22)
insert into b values (122)
select id
from a
where val in (select Id from b)
group by id
having count(id) = (select count(*) from b)