如何使用COUNT
关键字计算此查询的结果?
SELECT COUNT (*)
返回错误的结果。
SELECT files.* FROM files
LEFT JOIN tags_files ON tags_files.id_file = files.id
LEFT JOIN tags ON tags.id = tags_files.id
WHERE tags.name LIKE '%search%' OR files.name LIKE '%search%'
答案 0 :(得分:2)
如果要计算结果,请尝试以下查询:
SELECT COUNT( files.id ) AS total_num FROM files
LEFT JOIN tags_files ON tags_files.id_file = files.id
LEFT JOIN tags ON tags.id = tags_files.id
WHERE tags.name LIKE '%search%' OR files.name LIKE '%search%'
答案 1 :(得分:1)
您可以使用rowCount或num_rows取决于您使用的内容,因此它只会计算查询返回的行数
示例:
<?php
//For PDO
$sel = $dbh->prepare("SELECT files.* FROM files LEFT JOIN tags_files ON tags_files.id_file = files.id LEFT JOIN tags ON tags.id = tags_files.id WHERE tags.name LIKE '%search%' OR files.name LIKE '%search%'");
$sel->execute();
$count = $sel->rowCount();
//For mysqli
$sel = "SELECT files.* FROM files LEFT JOIN tags_files ON tags_files.id_file = files.id LEFT JOIN tags ON tags.id = tags_files.id WHERE tags.name LIKE '%search%' OR files.name LIKE '%search%'";
$result = mysqli_query($con,$sel);
$count = mysqli_num_rows($result);
?>