2013-02-14 86 views
0
###user_name  mobile  product_type### 
aaa 12345 1 

aaa 12345 2 

bbb 56789 2 

ccc 23456 2 

bbb 56789 1 

ccc 23456 1 

ddd 11111 1 

eee 22222 2 

我的db.table以上述格式.....其中2代表1的副本(移动和名称shuold都是唯一的)它必须具有原始product_type 1 .... I希望## EEE 22222 2 ##的记录..如果你发现它没有PRODUCT_TYPE 1..hope你懂的...我想NOT IN SQL QUERY SET

SELECT * FROM `applications` a where a.product_type=2 and user_name in (select user_name from `partners_applications` where user_name=a.user_name) order by a.user_name 

我知道这是不对的,你们可以请帮助我解决...

+0

你期望得到什么结果?你可以发布prtners表 – 2013-02-14 16:57:54

回答

0

你可以试试这个:

SELECT * 
    FROM (SELECT *, count(*) AS counter 
      FROM applications 
     GROUP BY user_name, mobile) aView 
WHERE product_type = 2 AND counter = 1; 
+0

我认为这对我有效..我无法评价这个职位...它要求一些声誉... – user1854007 2013-02-14 17:29:43

+0

@ user1854007你可以接受[一个令人满意的答案](http:// stackoverflow。 com/faq#howtoask)。 – SparKot 2013-02-14 17:33:29

0

我觉得你找这个

 SELECT * FROM `applications` a 
    INNER JOIN `partners_applications` pa ON pa.user_name=a.user_name 
    WHERE a.product_type=2 
    GROUP BY a.user_name 
    ORDER BY a.user_name 

编辑

因您的错字邪在这里我更新的答案

SELECT * FROM `partners_applications` a 
    where a.product_type=2 
    and a.user_name not in (select user_name from partners_applications 
        where product_type = 1) 
    group by a.user_name 
    order by a.user_name 

SQL DEMO HERE

EDIT2。

好,如果你有user_name具有不同的电话号码,然后你可以试试这个

SELECT * FROM `partners_applications` a 
    where a.product_type=2 
    and a.user_name not in (select user_name from partners_applications 
        where product_type = 1) 

    order by a.user_name ; 

DEMO SQL FIDDLE

+0

对不起,我的拼写错误都是同一张表,它应该读取为SELECT * FROM'partners_applications' a其中a.product_type = 2和user_name in(从“partners_applications”中选择user_name = a.user_name)order by a.user_name – user1854007 2013-02-14 17:09:13

+0

我只需要## eee 22222 2 ##的记录..如果你发现它没有product_type 1 .. – user1854007 2013-02-14 17:17:25

+0

编辑我的答案,希望是你需要的 – 2013-02-14 17:24:31

0

让我看看,如果我得到这个权利。您想要查看所有只有一种产品类型等于2的user_name | mobile对(否则,您将显示用户名ddd,对不对?)。

这是为你做的那个查询。

select user_name, mobile, max(product_type) product_type 
from table1 
group by user_name, mobile 
having count(distinct product_type) = 1 and sum(product_type = 2) = 1 

演示here

注意:我注意到您还想在结果中看到产品类型。但是,这应该是不必要的,因为初始条件是结果只有product_type = 2条记录。无论如何,我添加了一个max函数,以避免不必要和昂贵的连接或子查询。