sql select语句根据其成员的活动状态列出公司

时间:2017-04-17 12:41:15

标签: mysql

我有这张表:

表:公司(companyId,...)

表:成员(memberId,company_id,...)

表:广告系列(campaignId,member_id,status,...)

我想获得所有公司的列表,其中没有成员正在运行有效广告系列(状态= 1)

这样的事情:

sql = "SELECT * 
FROM company,members 
WHERE companyId = company_id AND memberId NOT IN 
   (SELECT member_id FROM campaigns WHERE status = 1)"

但我的问题是有些同胞没有会员。这家公司也应该上市

我无法理解这一点 - 任何人都可以帮助我吗?

5 个答案:

答案 0 :(得分:1)

SELECT * from company
WHERE not exists (
    SELECT campaigns.campaignId
    FROM campaigns JOIN members on (members.memberId = campaigns.member_id)
    WHERE STATUS = '1' and members.company_id = company.companyId   
)

答案 1 :(得分:0)

试试这个:

SELECT c.*
FROM company c
LEFT JOIN members m ON c.companyId = company_id
WHERE m.memberID IS NULL OR m.memberId NOT IN (
 SELECT m1.memberId
 FROM members m1 LEFT JOIN campaigns c ON m1.memberId = c.member_id
 WHERE c.status = 1
);

答案 2 :(得分:0)

请尝试以下方法:

SELECT c.companyId FROM company c LEFT JOIN members m ON
c.companyId = m.company_id INNER JOIN
campaigns cmp ON m.memberId = cmp.member_Id 
WHERE cmp.status NOT IN (1)

答案 3 :(得分:0)

这应该有效

    SELECT * FROM company,members 
    WHERE companyId = company_id AND 
    (
        memberId NOT IN  (SELECT member_id FROM campaigns WHERE status = 1) 
        OR memberId IS NULL OR memberId = ''
    )

答案 4 :(得分:0)

这个查询类似于嵌套for循环,它不适合大型表格。

从companyId IN公司中选择*( 从memberId IN的成员中选择company_id( 从其中status = 0且member_id NOT IN的广告系列中选择member_id(从status = 1的广告系列中选择member_id));