从中选择不包含信息

时间:2013-09-18 07:55:19

标签: mysql psql

让我试着重新解释我想要做的事情。首先,我有一个数据库或大型,50k及以上的物品,每天都在增长。我需要根据它没有的内容从这个数据库中提取信息。 (我正在使用SQL; pSQL或MySql并不重要。)

所以数据库看起来像这样(粗略地)

SELECT * FROM my_table
+---------+--------+
| ITEM    | TYPE   | 
+---------+--------+
|    12EU | P      |
|    12EU | R      | 
|    12EU | T      |
|    34RE | P      |
|    34RE | R      |
|    34RE | T      |
|    54TR | R      |
|    54TR | T      |
+---------+--------+

并非所有项目都有“P”,但它们都有“T”和“R”。我需要拉出没有TYPE“P”的ITEM,其他字母的ITEM对我来说无关紧要。

在这个例子中我想要ITEM“54TR”,因为它没有TYPE“P”。

我希望这有助于更好地解释我想要做的事情。

我所做的事情的例子:

SELECT distinct ITEM 
FROM (SELECT distinct ITEM FROM my_table WHERE TYPE='P') q 
WHERE TYPE!='P'
AND ITEM != q.ITEM
ORDER BY ITEM

这不起作用,它仍会返回其他类型,而不是正确的轨道......

4 个答案:

答案 0 :(得分:2)

select distinct item from yourtablename where type!='P'
minus
select distinct item from yourtablename where type='P'

示例:

create table myt (item varchar2(10),atype varchar2(10),piece varchar2(10));
insert into myt values ('12EU','P','ext');
insert into myt values ('12EU','R','ext');
insert into myt values ('34RE','T','ext');
insert into myt values ('54RT','P','ext');
insert into myt values ('34EU','R','ext');
insert into myt values ('54TR','P','ext');
commit;

SQL> select * from myt;

ITEM       ATYPE      PIECE
---------- ---------- ----------
12EU       P          ext
12EU       R          ext
34RE       T          ext
54RT       P          ext
34EU       R          ext
54TR       P          ext

6 rows selected.

SQL>从myt中选择不同的项目atype!='P'   2减   3从myt中选择不同的项目,其中atype ='P';

ITEM
----------
34EU
34RE

答案 1 :(得分:0)

select * from my_table t
where type != 'P'
AND not exists 
        (select 1 from my_table t2 where t2.item = t.item and t2.type = 'P')

答案 2 :(得分:0)

尝试使用SQL查询

select * from yourtablename where Type  not like '%P%'

答案 3 :(得分:0)

Select * from tablename where type != 'p'
相关问题