通用SQL问题

时间:2009-01-24 06:35:03

标签: sql

我有一张桌子A

ID    Term
10    A
10    B
10    C
20    A
20    B
20    E

将sql写入

的最佳方法是什么
  • 获取ID 10,如果我尝试找到(A,B,C)
  • 如果我试图找到(A,B)
  • ,请不要这样做
  • 如果我尝试在(C,D)
  • 中找到NOT,则获取ID 20

Select distinct ID from TableA where Term in (A,B,C) will return both 10 and 20

Select distinct ID from TableA where Term in (A,B) will also return both 10 and 20

Select distinct ID from TableA where Term NOT in (C,D) will also return both 10 and 20

谢谢!

4 个答案:

答案 0 :(得分:6)

1

SELECT ID 
FROM TableA
WHERE Term IN ('A','B','C')
GROUP BY ID
HAVING COUNT(ID)=3
LIMIT 1

这里3是集合的长度(在这种情况下为A,B,C)。 2& 3可能是上述的一些变化。

答案 1 :(得分:2)

我假设你想要一种形式的查询,可用于回答所有三个问题,而不是针对每个问题的不同类型的查询。

这些解决方案利用COUNT()不计算NULL s。如果OUTER JOINt2中的行不匹配,则会为NULL中的所有列生成t2

获取ID 10,如果我尝试查找(A,B,C)

ID 10有三个不同的术语值,我们正在搜索这三个术语。

SELECT t1.ID
FROM TableA t1 LEFT OUTER JOIN TableA t2 
  ON (t1.ID = t2.ID AND t1.term = t2.term AND t2.term IN ('A', 'B', 'C'))
GROUP BY t1.ID
HAVING COUNT(t1.term) = COUNT(t2.term);

如果我试图找到(A,B)

,请不要这样做

ID 10和ID 20都有三个不同的术语值,但我们的搜索只有两个。两个ID的计数均为3 = 2,因此两者都没有相同的计数。

SELECT t1.ID
FROM TableA t1 LEFT OUTER JOIN TableA t2 
  ON (t1.ID = t2.ID AND t1.term = t2.term AND t2.term IN ('A', 'B'))
GROUP BY t1.ID
HAVING COUNT(t1.term) = COUNT(t2.term);

获取ID 20,如果我尝试在(C,D)中找到NOT

ID 20有三个不同的术语值,所有三个都不是'C'或'D'。因此,计数是相等的。

SELECT t1.ID
FROM TableA t1 LEFT OUTER JOIN TableA t2 
  ON (t1.ID = t2.ID AND t1.term = t2.term AND t2.term NOT IN ('C', 'D'))
GROUP BY t1.ID
HAVING COUNT(t1.term) = COUNT(t2.term);

答案 2 :(得分:0)

Q1

SQLite version 3.6.10
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table A(ID smallint, term varchar(1));
sqlite> insert into A values(10, 'A');
sqlite> insert into A values(10, 'B');
sqlite> insert into A values(10, 'C');
sqlite> insert into A values(20, 'A');
sqlite> insert into A values(20, 'B');
sqlite> insert into A values(20, 'E');
sqlite> SELECT ID FROM A WHERE TERM = 'A' INTERSECT SELECT ID FROM A WHERE TERM
= 'B' INTERSECT SELECT ID FROM A WHERE TERM = 'C';
10

Q2

sqlite> SELECT ID FROM A WHERE TERM = 'A' EXCEPT SELECT ID FROM A WHERE TERM = '
B';

不会返回任何结果。在某些版本的SQL中,EXCEPT也可以称为MINUS

Q3

sqlite> SELECT ID FROM A EXCEPT SELECT ID FROM A WHERE TERM = 'C' UNION SELECT I
D FROM A WHERE TERM = 'D';
20

答案 3 :(得分:0)

CREATE TABLE mySearch (
    search_id       INT,
    val             CHAR(1),
    is_required     INT,
    is_excluded     INT
    )  

INSERT INTO mySearch VALUES (1, 'A', 1, 0)   -- Search1 : A is required  
INSERT INTO mySearch VALUES (1, 'B', 1, 0)   -- Search1 : B is required  
INSERT INTO mySearch VALUES (1, 'C', 1, 0)   -- Search1 : C is required  

INSERT INTO mySearch VALUES (2, 'A', 1, 0)   -- Search2 : A is required  
INSERT INTO mySearch VALUES (2, 'B', 1, 0)   -- Search2 : B is required  

INSERT INTO mySearch VALUES (3, 'C', 0, 1)   -- Search3 : C is excluded
INSERT INTO mySearch VALUES (3, 'D', 0, 1)   -- Search3 : D is excluded  

SELECT
    [search].search_id,
    [data].id
FROM
    TableA            AS [data]
LEFT JOIN
    my_search         AS [search]
        ON [search].val = [data].Term
GROUP BY
    [search].search_id,
    [data].ID
HAVING
    ISNULL(SUM([search].is_included),0) = (SELECT SUM(is_included) FROM mySearch WHERE search_id = [search].search_id)
    AND MAX([search].is_excluded) IS NULL

应该在一个查询中满足所有三个搜索。不幸的是,我无法测试它,因为我在公司的房子,他们没有极客玩具可以测试;)