我正在尝试按project_id分组的每个项目从列投票中计算“是”投票。投票可以是“是”,“否”以及为布尔值的空值。我使用“喜欢”运算符。但它抛出错误this is my table
这是代码
$sql = "SELECT COUNT(vote) as count,project_id FROM tbl_vote group by project_id where vote like '_Y'";
$result = $conn->query($sql); if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "count: " . $row["count"]. " project_id:".$row["project_id"]." <br>";
}
}
else {
echo "0 results";
}
条件错误
警告:mysqli_num_rows()期望参数1为mysqli_result,在第18行的C:\ xampp \ htdocs \ selection \ compute_sums.php中给出的布尔值''
答案 0 :(得分:0)
您将分组依据子句放在where子句之前
$sql = "SELECT COUNT(vote) as count,project_id FROM tbl_vote where vote = 'Yes' group by project_id";
SQL子句的顺序如下:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
SQL_NO_CACHE [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
[HAVING where_condition]
[WINDOW window_name AS (window_spec)
[, window_name AS (window_spec)] ...]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]
| LOCK IN SHARE MODE]]