即使返回的连接结果为空,mysqli_num_rows也会返回1

时间:2016-03-24 17:42:21

标签: mysql

这是我的疑问:

$query = 'SELECT b.author as author, b.review as review FROM location_final_similar a LEFT JOIN location_reviews b ON (a.uuid = b.uuid AND a.originalLocationID = b.originalLocationID) WHERE a.finalLocationID = "'.$locationID.'"';

正如您在上面所看到的,我唯一关心的是获取作者和评论(从表location_reviews中检索)。

当我使用$ locationID测试其中包含location_reviews内的数据时,mysqli_num_rows将返回正确的计数。

但是当我使用$ locationID测试时,location_reviews中没有数据,mysqli_num_rows仍然会返回1,但是作者和&审查null。那我怎么能避免这个呢?如果没有给定ID的评论,我需要mysqli_num_rows返回0。

1 个答案:

答案 0 :(得分:1)

您必须使用INNER JOIN(或者RIGHT JOIN),例如

SELECT b.author as author, b.review as review FROM location_final_similar a INNER JOIN location_reviews b ON (a.uuid = b.uuid AND a.originalLocationID = b.originalLocationID) WHERE a.finalLocationID = 1

请参阅Difference between INNER and OUTER joins以获得更好的解释。