我有两个MySQL表,
student = 150(代表学生总数的行总数)
雇主= 230(代表雇主总数的行数总数)
完整代码
<?php echo mysqli_num_rows($studempl); ?>
$conn = new mysqli ('localhost', 'root', '', 'dashboard');
$studempl = $conn ->query ("SELECT (SELECT COUNT(*) FROM student) + (SELECT COUNT(*) FROM employer) FROM dual");
$tot_studempl = mysqli_num_rows($studempl);
结果:1
我该如何解决这个问题?
我尝试使用
SELECT 'student' AS stdID, COUNT(*) FROM student
UNION
SELECT 'employer' AS emplID, COUNT(*) FROM employer*
显示结果= 2,
我还尝试使用:
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*) FROM employer) AS count2 FROM dual*
显示结果= 1,
我想我的代码可能有问题, 如何从学生和雇主处获得总数,以便我可以查看数据并在饼图中显示?
150,230
答案 0 :(得分:1)
答案 1 :(得分:0)
你的第二个查询应该有效。
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*)
FROM employer) AS count2 FROM dual* <-- make sure you remove (*)
// so it should be like this
SELECT (SELECT COUNT(*) FROM student) AS count1,(SELECT COUNT(*)
FROM employer) AS count2 FROM dual
然后你只需要获取数组来显示结果
$sql = "SELECT (SELECT COUNT(*) FROM playlists) AS count1,(SELECT COUNT(*) FROM artists) AS count2 FROM dual";
if ($result=mysqli_query($con,$sql))
{
$row = mysqli_fetch_array($result);
var_dump($row); // just checking the raw array...
echo "<br />";
echo $row[0]. ", ". $row[1]; // got your results -> 150, 230
}
mysqli_close($con);
另外,如果你将+替换为
,那么你的第一个查询是有效的$sql = "SELECT (SELECT COUNT(*) FROM students), (SELECT COUNT(*) FROM employer) FROM dual";