比较SQL Server中列的值

时间:2015-11-19 11:14:41

标签: sql-server

表1列是:

======================
A        B
======================
100      4
-----------
101      2
101      3
101      4
-----------
102      6
----------- 
103      7
-----------
104      2
104      3
104      4
-----------
105      2
-----------
106      4
-----------
107      3
--------------------------------

现在我输入了B参数,如'6'或'2,3,4'。

我想得到这样的结果:

如果输入B参数为'6',则输出应为:

======================
A        B
======================
102      6

如果输入B参数为'2,3,4',则输出应为:

======================
A        B
======================
101      2
101      3
101      4
-----------
104      2
104      3
104      4

3 个答案:

答案 0 :(得分:0)

只需选择所需的列:

SELECT A, B
FROM Table_1
WHERE B IN (2,3,4)

编辑: 如果只有所有参数存在时才需要它们

    SELECT A, B
    FROM Table_1
    WHERE A IN (SELECT A FROM Table_1 WHERE B = 2) AND A IN (SELECT A FROM Table_1 WHERE B = 3) AND A IN (SELECT A FROM Table_1 WHERE B = 4)

答案 1 :(得分:0)

根据您的具体要求,您可以尝试这个 -

- =之后的最后一位数,即3应该被检查的输入数量替换为我们的情况,它是3(2,3,4)

SELECT A,B FROM Table_1 AS tbl1 WHERE B IN (2,3,4) AND (select COUNT(1) FROM Table_1 AS tbl2 WHERE B IN (2,3,4) AND tbl1.A = tbl2.A)
 = 3

拜托,看看。

谢谢!

答案 2 :(得分:0)

SELECT A
    ,B
FROM table_1
WHERE A IN (
        SELECT A
        FROM table_1
        WHERE B = 2
        )
    AND A IN (
        SELECT A
        FROM table_1
        WHERE B = 3
        )