请问我是否可以合并来自不同变量的查询,或者是否有任何其他方式。我想将 $query_1
插入到 $query_2
示例:
$sql = "SELECT l_id from mlk";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$l_id[] = $row["l_id"];
}
}
mysqli_free_result($result);
foreach($l_id as $row){
$query_1= "(select count(*) from staff s where s.l_id = $row) as mlk_$row,";
}
$query_2= "SELECT mlk.lokasi,
//insert $query_1 here
from mlk INNER JOIN staff WHERE staff.l_id=mlk.l_id GROUP BY mlk.lokasi";
$result = mysqli_query($conn, $sql);
mlk
是一个存储人员工作地点的表$query_2
将显示位置名称,我还想显示该特定位置的员工总数。例如,吉隆坡,10
最佳答案
只在第二个查询中包含计数:
SELECT mlk.lokasi, count(*) as no_of_staff
from mlk
INNER JOIN staff on staff.l_id=mlk.l_id
GROUP BY mlk.lokasi
我还通过将连接条件从 where
子句移动到 on
子句,将隐式连接更改为显式连接。
关于PHP MYSQL - 合并来自另一个变量的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570358/