2014-01-23 35 views
0

我有一个表像下面选择栏不具有一定的排

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哪些有5| 1| 1(数|状态| uid)

但没有3| 0| 1| 1(number | status | uid | value)。

所以,如果我在完整的表上运行该查询,那么我应该只得到58944的结果。

回答

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) 

编辑:我想我可能误解了你的问题。以下查询会让您的ID为5| 1| 1 (number| status| uid),但不是3| 0| 1| 1 (number| status| uid| value)

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

+0

它仍然选择那些'id'有5,1,1和3,0,1,1。 –

0

或者只是:

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

我不认为有必要排除3 | 0 | 1 | 1时5纳入标准| 0 | 1是具体和专门的...

+0

如果你在上面看到,5,1,1在两个表中,所以我会得到两个结果。我只想要那些有5,1,1但没有3,0,1,1的 –

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) 
) 
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) 
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)