开发者

Selecting a GROUP BY statement only when a predicate is true on all grouped fields

开发者 https://www.devze.com 2023-04-02 05:15 出处:网络
I have a table like this: ------------- id|A| ------------- |1|2| |1|5| |1|6| |2|6| |2|9| ------------- How can I select all ids that have values of A that don\'t contain certain numbers, like I

I have a table like this:

-------------
| id  |  A  |
-------------
|  1  |  2  |
|  1  |  5  |
|  1  |  6  |
|  2  |  6  |
|  2  |  9  |
-------------

How can I select all ids that have values of A that don't contain certain numbers, like I don't w开发者_JS百科ant any id that have A IN (9,-1,20), for example?

Thank you!


Supposing you want only ID 1 because for ID 2 there is a value of A which is IN (9,-1,20) this is what you want:

SELECT t1.id, t1.A
  FROM my_table t1
  LEFT JOIN my_table t2
    ON t2.id = t1.id
   AND t2.A IN (9, -1, 20)
 WHERE t2.id IS NULL

or

SELECT id
  FROM my_table t1
 WHERE NOT EXIST (
       SELECT NULL
         FROM my_table t2
        WHERE t2.id = t1.id
          AND t2.A IN (9, -1, 20)
       )
0

精彩评论

暂无评论...
验证码 换一张
取 消