MySql子选择havning where子句,其中包含来自main select的值

时间:2015-01-09 10:39:58

标签: mysql

选择我正在努力工作

SELECT DISTINCT a.client_key, client_name 
FROM Bloggers AS a 
LEFT JOIN BloggersPosts AS b 
ON a.client_key = b.client_key 
WHERE a.status = 1 AND 
0 NOT IN (SELECT MIN(STATUS) FROM BloggersPosts AS c WHERE c.client_key=a.client_key)

由于某种原因,0 NOT IN (SELECT MIN(STATUS) FROM BloggersPosts AS c WHERE c.client_key=a.client_key)无法正常工作,有什么想法可以让它发挥作用吗?

编辑:通过不工作我的意思是,如果我删除susbelect - 我的查询给出结果行。但是一旦我添加它 - 结果就是空的。同时当我单独执行子选择SELECT MIN(STATUS) FROM BloggersPosts时 - 它返回1,这意味着将其作为子选择 - 也应该返回结果,但它不会返回结果。

谢谢

2 个答案:

答案 0 :(得分:0)

我希望这能完成你的工作。为您的问题尝试不同的解决方案。

SELECT DISTINCT a.client_key, client_name 
FROM Bloggers AS a 
JOIN (SELECT client_key, MIN(STATUS) minstatus FROM BloggersPosts GROUP BY client_key) b
ON a.client_key = b.client_key AND minstatus <> 0
WHERE a.status = 1

答案 1 :(得分:0)

使用JOIN,类似的东西:

SELECT DISTINCT a.client_key, client_name, MIN(c.STATUS) blog_status
FROM Bloggers AS a 
LEFT JOIN BloggersPosts AS b ON a.client_key = b.client_key 
LEFT JOIN BloggersPosts AS c ON c.client_key = a.client_key
WHERE a.status = 1 
HAVING blog_status <> 0