0

抱歉,标题不明确。

我在 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......|  
+++++++++++++++++++++++++++++++++  

我需要的是获取“线程”表的所有检查(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.......|  
++++++++++++++++++++++++++++++++++++  

谢谢,

4

2 回答 2

1

试试这个:

LEFT JOIN评论ON thread.id = comment.thread_id AND comment.ok = '1'

于 2012-07-28T15:52:35.063 回答
0
select thread.*, sum( comment.ok = 1) as countComment 
from `thread`
left join `comment` on thread.id=comment.thread_id
where thread.ok='1'
group by thread.id

count用条件替换sum

于 2012-07-28T15:51:41.743 回答