查询匹配列表SQL Server

时间:2017-05-18 10:24:03

标签: c# sql sql-server sql-server-2008

我有一张桌子bawe_services。我想获取与给定键匹配的所有数据

就像我有字段

id  | Service_id |bawe_id
1       2          2
2       3          3
3       2          3

如果我传递service = 2我需要service_id = 2的所有记录,如果我传递service = 1,2,3而不是我想要0行,因为任何bawe都没有给出1个服务。我有0行。

我使用此查询

select * from aspnet_bawe_services where ser_id in(1,2,3)

提前完成

4 个答案:

答案 0 :(得分:2)

" in"中的参数计数声明必须与具有相同数字的声明匹配。

select bawe_id from [dbo].[aspnet_bawe_services]
where Service_id in (2)
group by bawe_id
having count(Service_id)=1;

bawe_id
-----------
2
3

select bawe_id from [dbo].[aspnet_bawe_services]
where Service_id in (2,3)
group by bawe_id
having count(Service_id)=2;

bawe_id
-----------
3

select bawe_id from [dbo].[aspnet_bawe_services]
where Service_id in (1,2,3)
group by bawe_id
having count(Service_id)=3;

bawe_id
-----------

(0 row(s) affected)

答案 1 :(得分:1)

尝试这个:这真的很乏味但是独特的要求我想要实现这一点,我们必须使用功能

1-Function返回service_id

的非重复计数

2 - 用于拆分逗号分隔值并以表格格式返回的函数

- 函数返回service_id

的非重复计数
setuptools

- 用于分割逗号分隔值并以表格格式返回的函数 - 从中​​复制的功能 - separate comma separated values and store in table in sql server

CREATE FUNCTION [dbo].[getCount](@service_id varchar(500))
RETURNS INT             
AS       
BEGIN   
    DECLARE @count int   

    SELECT @count = COUNT(DISTINCT(t.service_id))
    FROM tmptos t
    INNER JOIN [dbo].[SplitValue](@service_id, ',') tt on t.service_id = tt.items

RETURN @count
END;

输出:

CREATE FUNCTION [dbo].[SplitValue](@String varchar(MAX), @Delimiter char(1))       
RETURNS @temptable TABLE (items VARCHAR(MAX))       
AS       
BEGIN      
    DECLARE @idx int       
    DECLARE @slice varchar(8000)       

    SELECT @idx = 1       
        if len(@String)<1 or @String is null  return       

    WHILE @idx!= 0       
    BEGIN       
        set @idx = charindex(@Delimiter,@String)       
        IF @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       

        IF(LEN(@slice)>0)  
            INSERT INTO @temptable(Items) values(@slice)       

        SET @String = right(@String,len(@String) - @idx)       
        IF LEN(@String) = 0 break       
    END   
RETURN 
END;

--Table with Sample Data

create table tmptos(id int, Service_id int, bawe_id int)
insert into tmptos values
(1,       2,          2),
(2,       3,         3),
(3,       2,          3)


declare @service_id varchar(50) = '2,3'

select *
from tmptos t
inner join [dbo].[SplitValue](@service_id, ',') tt on t.Service_id = tt.items
where [dbo].[getCount](@service_id) = (select count(distinct(items)) from [dbo].[SplitValue](@service_id, ','))

它有点冗长,但效果很好。

答案 2 :(得分:0)

select * from aspnet_bawe_services 
where Service_id  in (1,2,3) 
and 
( select count(distinct Service_id) from aspnet_bawe_services where Service_id  in (1,2,3) ) = 3

查询中的最后一个数字(在本例中为“3”)是元素计数,您在IN列表中有。

答案 3 :(得分:0)

您可以使用group byhaving获取所需的服务ID:

select service_id
from t
where bawe_id in (1, 2, 3)
group by service_id
having count(distinct bawe_id) = 3;

&#34; = 3&#34;是IN列表中的ID数。

然后,您可以使用injoinexists来获取完整记录:

select t.*
from t
where t.service_id in (select service_id
                       from t
                       where bawe_id in (1, 2, 3)
                       group by service_id
                       having count(distinct bawe_id) = 3
                      );