嗨,伙计们,我正在尝试使用更有效的东西,而不是使用来自多个查询的mysql_num_rows
。我是否可以使用一个查询,例如,如果$type=="a"
,则根据条件对mysql_num_rows()
进行计数?并为每个声明变量。对于 B 行、C 行、D 行等。谢谢
$rows_a = mysql_query("SELECT $type FROM table WHERE $type = 'a'");
$a_rows = mysql_num_rows($rows_a);
$rows_b = mysql_query("SELECT $type FROM table WHERE $type = 'b'");
$b_rows = mysql_num_rows($rows_b);
$rows_c = mysql_query("SELECT $type FROM table WHERE $type = 'c'");
$c_rows = mysql_num_rows($rows_c);
$rows_d = mysql_query("SELECT $type FROM table WHERE $type = 'd'");
$d_rows = mysql_num_rows($rows_d);
SELECT COUNT(id), type FROM table GROUP BY type
可能是这样的:(如果没有主 ID 列)
SELECT COUNT(*) as num_rows, type FROM table WHERE type IN ('a', 'b', 'c', 'd') GROUP BY type
或动态:
SELECT COUNT(*) as num_rows, type FROM table WHERE type IN ('select types from a sub query') GROUP BY type