我正在尝试使用2个连接进行查询,这两个连接都需要WHERE子句。我注意到这不起作用,我只能在连接的末尾添加一个WHERE。所以我想知道如何才能做到最好..
这是我当前的查询,它不接受第一个连接中的WHERE:
SELECT
p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT(c.id) AS answers
FROM
posts_tbl AS p
LEFT JOIN
post_reply_tbl AS c ON c.post_id = p.id WHERE c.type = 1 AND c.type = 3
LEFT JOIN
post_reply_tbl AS a ON a.post_id = p.id WHERE a.type = 2
GROUP BY p.id
答案 0 :(得分:5)
将其更改为AND
而不是WHERE
:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT(c.id) AS answers
FROM posts_tbl AS p
LEFT JOIN post_reply_tbl AS c
ON c.post_id = p.id
AND c.type = 1 AND c.type = 3
LEFT JOIN post_reply_tbl AS a
ON a.post_id = p.id
AND a.type = 2
GROUP BY p.id
我认为您需要稍微改变一下,因为c.type
不能同时拥有两个值:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT(c.id) AS answers
FROM posts_tbl AS p
LEFT JOIN post_reply_tbl AS c
ON c.post_id = p.id
AND c.type IN (1, 3) -- changed to use an OR
LEFT JOIN post_reply_tbl AS a
ON a.post_id = p.id
AND a.type = 2
GROUP BY p.id
如果您希望c.type
同等于1
和3
,那么您可能需要考虑使用:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, c.answers
FROM posts_tbl AS p
LEFT JOIN
(
select COUNT(c.id) answers, c.post_id
from post_reply_tbl c
where c.type in (1, 3)
group by c.post_id
having COUNT(distinct type) = 2
) AS c
ON c.post_id = p.id
LEFT JOIN post_reply_tbl AS a
ON a.post_id = p.id
AND a.type = 2
关于您的查询的后续注释,因为您只在一列上使用GROUP BY
,MySQL决定返回其他列的值,并且值可能是意外的。您可以确定返回值的唯一方法是按它们聚合每个列或组。 (见MySQL Extensions to GROUP BY)
来自MySQL文档:
MySQL扩展了GROUP BY的使用,因此选择列表可以引用GROUP BY子句中未命名的非聚合列。 ...您可以通过避免不必要的列排序和分组来使用此功能来获得更好的性能。但是,当GROUP BY中未命名的每个非聚合列中的所有值对于每个组都相同时,这非常有用。服务器可以自由选择每个组中的任何值,因此除非它们相同,否则所选的值是不确定的。此外,添加ORDER BY子句不会影响每个组中值的选择。选择值后会对结果集进行排序,而ORDER BY不会影响服务器选择的值。
出于这个原因,最好使用子查询来获取count()
,然后您将确保返回其余列的正确结果。:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, c.answers
FROM posts_tbl AS p
LEFT JOIN
(
select COUNT(c.id) answers, c.post_id
from post_reply_tbl c
where c.type in (1, 3)
) AS c
ON c.post_id = p.id
LEFT JOIN post_reply_tbl AS a
ON a.post_id = p.id
AND a.type = 2
答案 1 :(得分:0)
您可以在查询结尾处添加所有where条件:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT(c.id) AS answers
FROM posts_tbl AS p
LEFT JOIN post_reply_tbl AS c ON c.post_id = p.id
LEFT JOIN post_reply_tbl AS a ON a.post_id = p.id
WHERE a.type = 2
AND c.type = 1 AND c.type = 3
GROUP BY p.id
或在连接条件中使用AND:
SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT(c.id) AS answers
FROM posts_tbl AS p
LEFT JOIN post_reply_tbl AS c ON c.post_id = p.id AND c.type = 1 AND c.type = 3
LEFT JOIN post_reply_tbl AS a ON a.post_id = p.id AND a.type = 2
GROUP BY p.id
答案 2 :(得分:0)
只需将所有条款放在where子句
中SELECT p.id, p.username, p.date, p.subject, p.year, p.brand,
p.model, p.type, p.bodywork, p.text, p.image, p.chassis, COUNT AS answers
FROM posts_tbl AS p
LEFT JOIN post_reply_tbl AS c ON c.post_id = p.id
LEFT JOIN post_reply_tbl AS a ON a.post_id = p.id
WHERE a.type = 2 AND c.type = 1 AND c.type = 3
GROUP BY p.id