1

我正在尝试使用 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
4

3 回答 3

5

将其更改为ANDnot a 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等于13,那么您可能需要考虑使用:

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只在一个列上使用 a,MySQL 决定返回其他列的哪些值并且这些值可能是意外的。您可以确定返回值的唯一方法是按它们聚合每个列或组。(参见MySQL 对 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
于 2013-02-26T16:50:11.293 回答
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
于 2013-02-26T16:51:41.463 回答
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
于 2013-02-27T08:05:14.690 回答