【问题标题】:What is the difference in mySQL AND vs WheremySQL AND 与 Where 有什么区别
【发布时间】:2023-02-25 17:27:07
【问题描述】:

我正在使用 mysql,但我混淆了“And”、“Where” Somby dy 可以告诉我这些之间有什么区别。

SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id AND comment.approve = 1 GROUP BY posts.post_id

SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id WHERE comment.approve = 1 GROUP BY posts.post_id

【问题讨论】:

  • 不要破坏你的问题。

标签: mysql


【解决方案1】:

它们不一样,第一个将返回所有的关联,第二个将只为 where 匹配中的行返回关联。

In this other duplicate question 你可以看到完整的解释和例子

SQL JOIN - WHERE clause vs. ON clause

【讨论】:

    【解决方案2】:

    只需将查询更改为使用这样的内部联接:

    select tableA.id, tableA.name, tableB.details 
    from tableA
    inner join tableB ...
    

    这是左连接的定义:

    The LEFT JOIN (also called LEFT OUTER JOIN) keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).
    

    而内部连接的定义是:

    The INNER JOIN keyword return rows when there is at least one match in both tables.
    

    【讨论】:

      猜你喜欢
      • 2011-11-18
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多