php - 如何解决这两个表之间的内连接问题?
问题描述
我有这个代码工作完美
<?php
require'dbehrpdo.php';
$seen = "2";
$count = 1;
$total = 0;
$t1 = strtotime($datea." 00:00:00");
$t2 = strtotime($dateb." 23:59:59");
foreach($conepdo->query("SELECT COUNT(id), school FROM waitinglist WHERE seen = '$seen' AND timeseen BETWEEN $t1 AND $t2 GROUP by school ORDER BY COUNT(id) DESC") as $row) {
echo "<tr>";
echo "<td width='10%'>" . $count . "</td>";
echo "<td width='70%'>" . $row['school'] . "</td>";
echo "<td width='20%'>" . $row['COUNT(id)'] . "</td>";
echo "</tr>";
$count = $count + 1;
$total = $total + $row['COUNT(id)'];
}
?>
列“学校”商店缩写,如 NG,美国,但我有另一张表“学校”
id、name 和 abr 作为全名和缩写。
为什么这段代码不起作用?
<?php
require'dbehrpdo.php';
$seen = "2";
$count = 1;
$total = 0;
$t1 = strtotime($datea." 00:00:00");
$t2 = strtotime($dateb." 23:59:59");
foreach($conepdo->query("SELECT COUNT(waitinglist.id), schools.name FROM waitinglist INNER JOIN schools ON waitinglist.school = schools.abr WHERE seen = '$seen' AND timeseen BETWEEN $t1 AND $t2 GROUP by school ORDER BY COUNT(id) DESC") as $row) {
echo "<tr>";
echo "<td width='10%'>" . $count . "</td>";
echo "<td width='70%'>" . $row['schools.name'] . "</td>";
echo "<td width='20%'>" . $row['COUNT(id)'] . "</td>";
echo "</tr>";
$count = $count + 1;
$total = $total + $row['COUNT(id)'];
}
?>
解决方案
只需为 SQL 查询中的列使用别名
<?php
require'dbehrpdo.php';
$seen = "2";
$count = 1;
$total = 0;
$t1 = strtotime($datea." 00:00:00");
$t2 = strtotime($dateb." 23:59:59");
$sql = "SELECT
COUNT(waitinglist.id) AS cnt,
schools.name AS school_name
FROM waitinglist
INNER JOIN schools ON waitinglist.school = schools.abr
WHERE seen = '$seen' AND timeseen BETWEEN '$t1' AND '$t2'
GROUP by schools.name
ORDER BY COUNT(id) DESC";
foreach($conepdo->query($sql) as $row) {
echo "<tr>";
echo "<td width='10%'>" . $count . "</td>";
echo "<td width='70%'>" . $row['school_name'] . "</td>";
echo "<td width='20%'>" . $row['cnt'] . "</td>";
echo "</tr>";
$count = $count + 1;
$total = $total + $row['COUNT(id)'];
}
?>
推荐阅读
- php - 每当我上传页面时,如何让我的页面停止输入“谷歌欺骗性网站”
- android - 我们可以识别视图是 Toast 还是 Dialog 吗?如果是,那么如何?
- android - 应用程序被杀死时广播接收器未调用服务
- c# - 在新选项卡/窗口中打开外部站点并传递数据
- character-encoding - Visual Studio Code - 使用 Diff 屏幕时 TFVC 项目中的文件编码
- ios - 使用呼叫目录扩展识别来电显示的问题
- wxwidgets - windows wxwidgets 应用程序中 wx.EVT_ACTIVATE 事件的默认行为是什么
- php - 连接到数据库mysql时出现错误消息
- asp.net - 在 VB.NET 中清除 IntPtr 目标
- r - 为什么在给定的 r 库中不能有多个版本的 r 包