选择那个没有特定行的列

时间:2014-01-23 16:54:53

标签: sql database postgresql

我有一张如下表格

   Column   |           Type           | Modifiers
------------+--------------------------+-----------
 id         | integer                  | not null
 number     | integer                  | not null
 status     | integer                  |
 uid        | integer                  |
 value      | integer                  |
 comment    | character varying(2000)  |
 date       | timestamp with time zone |

查询:select * from table_name where id like '%58943';

结果:

        id| number|     status |     uid |       value| comment  | date
----------+-------+------------+---------+------------+----------+-------------------------------
    58943 |     5 |          1 |       1 |            |          | 2014-01-23 14:24:34.708676+01
    58943 |     3 |          0 |       1 |          1 |          | 2014-01-23 14:23:46.740663+01
    58943 |     3 |          0 |       1 |          4 | [admin]  | 2014-01-23 14:24:34.505752+01
    58943 |     3 |          0 |     974 |          4 | [admin]  | 2014-01-23 14:24:34.601017+01
    58943 |     3 |          0 |     977 |          4 | [admin]  | 2014-01-23 14:24:34.708676+01
    58943 |     2 |          0 |       1 |            | ver 12   | 2014-01-23 14:22:01.298001+01
    58943 |     1 |          0 |       1 |            |          | 2014-01-23 14:22:01.052535+01
(7 rows)

查询:select * from table_name where id like '%58944';

结果:

            id| number|     status |     uid |       value| comment  | date
----------+-------+------------+---------+------------+----------+-------------------------------
        58944 |     5 |          1 |       1 |            |                   | 2014-01-23 14:25:34+01
        58944 |     3 |          0 |     977 |          4 | looks fine        | 2014-01-23 14:25:34+01
        58944 |     3 |          0 |     974 |          4 | Approve all conff | 2014-01-23 14:25:34+01
        58944 |     2 |          0 |       1 |            | vers 12           | 2014-01-23 14:22:11.86668+01
        58944 |     1 |          0 |       1 |            |                   | 2014-01-23 14:22:11.857947+01
    (5 rows)

问题:我正在尝试找到id {number | status | uid)

5| 1| 1

但没有3| 0| 1| 1(数字|状态| uid |值)。

因此,如果我在完整的表上运行该查询,那么我应该只得到结果中的58944

5 个答案:

答案 0 :(得分:2)

以下查询应该适合您。

SELECT id FROM table_name 
WHERE (number = 5 AND status = 1 AND uid = 1) 
    AND NOT (number = 3 AND status = 0 AND uid = 1 AND value = 1)

编辑:我想我可能误解了你的问题。以下查询将为您提供5| 1| 1 (number| status| uid)但不包含3| 0| 1| 1 (number| status| uid| value)的ID。

SELECT * FROM tab a 
WHERE (a.number = 5 AND a.status = 1 AND a.uid = 1) 
    AND NOT EXISTS (SELECT id FROM tab t 
                    WHERE t.id = a.id AND t.number = 3 AND t.status = 0 
                        AND t.uid = 1 AND t.value = 1)

检查这个小提琴SQL FIDDLE

答案 1 :(得分:0)

或者只是:

SELECT id FROM table_name 
WHERE (number = 5 AND status = 1 AND uid = 1) 

当5 | 0 | 1的包含标准是特定且独占时,我认为不需要排除3 | 0 | 1 | 1 ...

答案 2 :(得分:0)

您需要使用not exists

select
  a.*
from
  table_name a
where
  (number = 5 and status = 1 and uid = 1)
  and not exists (
    select 1
    from 
      table_name b
    where 
      b.id = a.id
      and (b.number = 3 and b.status = 0 and b.uid = 1)
  )

答案 3 :(得分:0)

修改 user2989408 以下答案可能有效。

SELECT id FROM table_name 
WHERE (id  like '%58943' AND number = 5 AND status = 1 AND uid = 1) 
AND NOT (number = 3 AND status = 0 AND uid = 1 AND value = 1)

答案 4 :(得分:0)

在上面的评论中误解了。这很简单:

SELECT 
  id 
FROM 
  table_name 
WHERE 
(number = 5 AND status = 1 AND uid = 1) 
and 
id not in (select id from table_name where number = 3 AND status = 0 AND uid = 1 and VALUE =1)