php - 我无法构建 SQL 查询(连接)

标签 php mysql join left-join

抱歉,标题不明确。

我正在 PHP 上使用 MySQL。

这些是我的表格:

+++++++++++++++++++++++  
|........thread.......|  
+++++++++++++++++++++++  
|.id.|..title..|..ok..|  
+++++++++++++++++++++++  
|.45.|.baslik.|...1...|  
|.53.|.baslik.|...0...|  
|.56.|.baslik.|...1...|  
+++++++++++++++++++++++  

+++++++++++++++++++++++++++++++++  
|............comment............|  
+++++++++++++++++++++++++++++++++  
|.id.|..text..|.ok.|.thread_id..|  
+++++++++++++++++++++++++++++++++  
|.1..|.falan..|.0..|....45......|  
|.3..|.filan..|.1..|....56......|  
+++++++++++++++++++++++++++++++++  

我需要的是获取“thread”表的所有已检查(ok=1)行,其行数提供 thread.id = comment.thread_id 和 comment.ok=1 的相等性。

当我使用以下查询时,我会得到选中或未选中的所有评论计数。

select thread.*, count( comment.thread_id ) as countComment from `thread` left join `comment` on thread.id=comment.thread_id where thread.ok='1' group by thread.id

我想要得到的输出必须如下所示。

++++++++++++++++++++++++++++++++++++  
|.id.|.title..|..ok.|.commentCount.|  
++++++++++++++++++++++++++++++++++++  
|.45.|.baslik.|...1.|......0.......|  
|.56.|.baslik.|...1.|......1.......|  
++++++++++++++++++++++++++++++++++++  

谢谢

最佳答案

试试这个:

LEFT JOINcommentON thread.id = comment.thread_id AND comment.ok = '1'

关于php - 我无法构建 SQL 查询(连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702447/

相关文章:

php - 连接两个表并仅输出第一个表中存在的行

Mysql 查询运行完美,但 JPA 查询没有返回任何结果

mysql - 为什么 SQL 语句产生了语法错误,但 SQL 注入(inject)却成功了?

MySQL - 列的条件连接

sql-server - 将相关表中的总和分组到 linq 查询列中

php - PHP 中 md5 加密的当前功能替代品是什么?

php - 如何获取最后插入的 id 以及如何使用 php 检查更新查询的条件

php - 如何使用 PHP 构建支持票系统?

mysql - SQL Server : Group by Replace NULL

mysql - 使用减法运算从两个表创建 View