如何将多个查询合并为一个?
How to combine multiple query's into one?
我是 运行 RGraph 框架上的折线图,我正在使用 SELECT COUNT 语句来表示拒绝、接受、批准等......计算有多少项目被拒绝或接受等然后将查询数据转储到一个数组中,但是我正在寻找一种更简单的方法来实现这个查询,而不是 运行 对每个唯一行值的查询,如果我必须遇到除了拒绝、接受等之外的其他列数据……我不会,那时我的代码似乎不太可扩展。请帮助
到目前为止,我运行查询每个关键字,希望我的代码能解释这一点。
最后一个变量是我提供给 RGRAPH 的变量,它可以很好地工作,但是如果我的行数据发生变化,这不是正确的方法,而且可扩展性不是很好。
<?php
$cxn = mysqli_connect("localhost","root","", "csvimport");
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion = 'rejected'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$rejected = $display[0];
//echo $rejected;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion =
'accepted'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$accepted = $display[0];
//echo $accepted;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion = '-'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$dash = $display[0];
//echo $dash;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion =
'approved'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$approved = $display[0];
//echo $approved;
$datarray = [$rejected, $accepted, $dash, $approved];
print_r($datarray);
$data_string = "[" . join(", ", $datarray) . "]";
echo "<br>";
print_r($data_string);
?>
您可以只使用 GROUP BY 并将结论列添加到结果集中,所以
SELECT conclusion, COUNT(*) as total
FROM table_1
WHERE conclusion in ('rejected', 'accepted', '-', 'approved')
GROUP BY conclusion
然后检索结果集的每一行
$totals = [];
while($row = mysqli_fetch_array($result)) {
$totals [$row[0]] = [$row[1]];
}
和 $totals
将是一个类似于
的数组
array( 'accepted' => 12,
'approved' => 20...)
如果你想要所有的结论,那么只需删除 WHERE conclusion in
行,它将 return 所有的可能性以及计数。
我是 运行 RGraph 框架上的折线图,我正在使用 SELECT COUNT 语句来表示拒绝、接受、批准等......计算有多少项目被拒绝或接受等然后将查询数据转储到一个数组中,但是我正在寻找一种更简单的方法来实现这个查询,而不是 运行 对每个唯一行值的查询,如果我必须遇到除了拒绝、接受等之外的其他列数据……我不会,那时我的代码似乎不太可扩展。请帮助
到目前为止,我运行查询每个关键字,希望我的代码能解释这一点。
最后一个变量是我提供给 RGRAPH 的变量,它可以很好地工作,但是如果我的行数据发生变化,这不是正确的方法,而且可扩展性不是很好。
<?php
$cxn = mysqli_connect("localhost","root","", "csvimport");
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion = 'rejected'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$rejected = $display[0];
//echo $rejected;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion =
'accepted'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$accepted = $display[0];
//echo $accepted;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion = '-'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$dash = $display[0];
//echo $dash;
$query = "SELECT COUNT(*) FROM table_1 WHERE conclusion =
'approved'";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$display = mysqli_fetch_array($result);
$approved = $display[0];
//echo $approved;
$datarray = [$rejected, $accepted, $dash, $approved];
print_r($datarray);
$data_string = "[" . join(", ", $datarray) . "]";
echo "<br>";
print_r($data_string);
?>
您可以只使用 GROUP BY 并将结论列添加到结果集中,所以
SELECT conclusion, COUNT(*) as total
FROM table_1
WHERE conclusion in ('rejected', 'accepted', '-', 'approved')
GROUP BY conclusion
然后检索结果集的每一行
$totals = [];
while($row = mysqli_fetch_array($result)) {
$totals [$row[0]] = [$row[1]];
}
和 $totals
将是一个类似于
array( 'accepted' => 12,
'approved' => 20...)
如果你想要所有的结论,那么只需删除 WHERE conclusion in
行,它将 return 所有的可能性以及计数。