2014-03-27 75 views
3

精確值我有這樣的表:搜索與SQL

a b 

8 7 
8 2 
9 7 
9 2 
9 3 

我希望得到一個「a」值,使得它具有精確的搜索值「B」。例如,如果我搜索(7,2),我希望查詢返回8,如果我搜索(7,2,3),我希望它返回9,否則它必須返回空值:

Search  Result 
(7,2)  8 
(7,2,3)  9 
(7,3)  Null 
(7,2,4)  Null 
... 

有沒有可能做到這一點,而不使用「group by」串聯?

[編輯]對於「由級聯組」我的意思是這樣的GROUP_CONCAT()在MySQL或然而任何字符串函數。

+1

數據庫使用的是? – KrazzyNefarious

+0

這個命令很重要嗎?換句話說,你是否在尋找匹配的「序列」?這就是我的意思:在你的例子中,你有(7,2,3)結果9.應該(2,7,3)導致什麼? –

+0

你究竟是什麼意思,以「串聯組合」 –

回答

0

group by /串聯方法的工作原理。另一種方法是group byhaving子句:

select a 
from table t 
group by a 
having sum(case when b = 7 then 1 else 0 end) = 1 and 
     sum(case when b = 2 then 1 else 0 end) = 1 and 
     sum(case when b not in (7, 2) then 1 else 0 end) = 0; 

的想法是計數匹配的每個值,該值的值。你實際上可以用字符串操作來做到這一點,但是數據庫之間的不同。下面是這樣使用in和價值觀,你的號碼的「一般」的方式尋找:

select a 
from table t 
group by a 
having count(*) = 2 and 
     count(distinct (case when b in (2, 7) then b end)); 
+0

實際上,OP要求解決方案*沒有*組。 –

+0

'和計數(不同(在(2,7)中b然後b結束的情況下))似乎是合適的條件 –

+0

@FabianBigler。 。 。用戶要求解決方案時沒有「通過串聯組」。使用「charindex」的 –

0

在SQL Server

SELECT a FROM tableName 
WHERE a NOT IN (SELECT a FROM ( 
           SELECT a, 
           CASE WHEN CHARINDEX(b,@searchTerm)=0 
           THEN 0 ELSE 1 END as Result 
           FROM tableName 
           )z 
        WHERE z.Result=0 
       ) 

我使用CHARINDEX()函數來檢查是否搜索長期存在。

+0

對於只有一個值的數字列沒有任何意義。 –

0

不知道你「通過級聯組」與意思 - 我不認爲這是可以做到沒有一個group by

您沒有指定您的DBMS,所以這是ANSI SQL:

with search_values (val) as (
    values (7), (2) 
) 
-- first part gets all those that do have the search values 
-- but will also include those rows that have more than the searched ones 
select a 
from data 
where b in (select val from search_values) 
group by a 
having count(distinct b) = (select count(*) from search_values) 

intersect 
-- the intersect then filters out those that have exactly the search values 

select a 
from data 
group by a 
having count(distinct b) = (select count(*) from search_values); 

SQLFiddle例如:http://sqlfiddle.com/#!15/dae93/1

使用CTE爲「搜索值」避免重複這些和並避免「硬編碼」將要搜索的項目數。如果你看看你7,2,3只是另一個值添加到CTE

而不是使用intersect它可以與合作相關子查詢重寫

with search_values (val) as (
    values (7), (2) 
) 
select d1.a 
from data d1 
where d1.b in (select val from search_values) 
group by d1.a 
having count(distinct d1.b) = (select count(*) from search_values) 
    and count(distinct d1.b) = (select count(*) from data d2 where d2.a = d1.a); 
0

另一種方法是使用INTERSECT

然而,第一種情況似乎是一個螺母,其仍然需要被破解(返回8,9代替8)。

這裏的SQL代碼:

--(7,2) => 8,9 (instead of 8) :(
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 7 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 2 

--(7,2,3) => 9 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 7 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 2 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 3 

--(7,4) => NULL 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 7 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 4 

--(7,2,4) => NULL 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 7 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 2 
    INTERSECT 
    SELECT distinct(a) 
    FROM t1 
    WHERE b = 4 

Fiddle Demo