Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当不存在左联接时,计数表上的行数返回1_Php_Sql_Left Join - Fatal编程技术网

Php 当不存在左联接时,计数表上的行数返回1

Php 当不存在左联接时,计数表上的行数返回1,php,sql,left-join,Php,Sql,Left Join,例如,我有两张桌子: 发布: id | author | content | date 1 | Lucas | Hello! | 2016 2 | Igor | Hi! | 2016 id | post_id | content | date 1 | 2 | hehehe | 2016 2 | 1 | hahaha | 2016 3 | 2 | huhuhu | 2016 评论: id | author | content

例如,我有两张桌子:

发布

id | author | content | date
1  | Lucas  | Hello!  | 2016
2  | Igor   | Hi!     | 2016
id | post_id | content | date
1  | 2       | hehehe  | 2016
2  | 1       | hahaha  | 2016
3  | 2       | huhuhu  | 2016
评论

id | author | content | date
1  | Lucas  | Hello!  | 2016
2  | Igor   | Hi!     | 2016
id | post_id | content | date
1  | 2       | hehehe  | 2016
2  | 1       | hahaha  | 2016
3  | 2       | huhuhu  | 2016
我要做一个
选择
,返回所有帖子,并用
post.id=comment.id
对所有评论行进行
计数

所以,我试着:

SELECT p.id, p.author, p.content, p.date, COUNT(*) AS numComments FROM post p LEFT JOIN comment ON p.id = post_id WHERE p.author = '$author' GROUP BY p.id DESC LIMIT 12
SELECT p.id, p.author, p.content, p.date, CASE WHEN COUNT(*) < 1 THEN '0' ELSE COUNT(*) END AS numComments FROM post p LEFT JOIN comment ON p.id = post_id WHERE p.author = '$author' GROUP BY p.id DESC LIMIT 12
我得去做。但是,即使不存在带有
p.id=post\u id
的注释,他也会返回1

所以,我试着:

SELECT p.id, p.author, p.content, p.date, COUNT(*) AS numComments FROM post p LEFT JOIN comment ON p.id = post_id WHERE p.author = '$author' GROUP BY p.id DESC LIMIT 12
SELECT p.id, p.author, p.content, p.date, CASE WHEN COUNT(*) < 1 THEN '0' ELSE COUNT(*) END AS numComments FROM post p LEFT JOIN comment ON p.id = post_id WHERE p.author = '$author' GROUP BY p.id DESC LIMIT 12
选择p.id、p.author、p.content、p.date、CASE WHEN COUNT(*)<1,然后选择“0”ELSE COUNT(*)作为numComments从p.id=post_id的post p LEFT JOIN comment中结束,其中p.author=“$author”GROUP BY p.id DESC LIMIT 12

但结果是一样的。如何做到这一点?

您可以通过这种方式获得计数,最后一种是按订单而不是按分组:

SELECT p.id, p.author, p.content, p.date, 
(select COUNT(*) from comment where p.id = comment.post_id) AS numComments FROM post p 
WHERE p.author = '$author' 
ORDER BY p.id DESC LIMIT 12

您可以通过这种方式获得计数,最后一种是按订单而不是按分组:

SELECT p.id, p.author, p.content, p.date, 
(select COUNT(*) from comment where p.id = comment.post_id) AS numComments FROM post p 
WHERE p.author = '$author' 
ORDER BY p.id DESC LIMIT 12

由于外部联接返回一行,即使没有匹配的数据,您也需要从内部表中计算一列,通常是联接中使用的列:

SELECT p.id, p.author, p.content, p.date, COUNT(post_id) AS numComments
FROM post p LEFT JOIN comment ON p.id = post_id 
WHERE p.author = '$author' 
GROUP BY p.id -- seems to be mysql, otherwise you need to add more columns to the list
如果不想显示计数为零的行,只需切换到
内部联接。

由于外部联接返回一行,即使没有匹配的数据,您也需要从内部表中计算列,通常是联接中使用的列:

SELECT p.id, p.author, p.content, p.date, COUNT(post_id) AS numComments
FROM post p LEFT JOIN comment ON p.id = post_id 
WHERE p.author = '$author' 
GROUP BY p.id -- seems to be mysql, otherwise you need to add more columns to the list
如果不想显示计数为零的行,只需切换到 内部连接