我试图在SELECT查询中交叉匹配2个表。此外,我还会过滤结果,以便在60天内仅回显记录。我使用PDO来做到这一点。
此代码有效:
//$my_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach ($my_con->query("SELECT subject, date, name FROM table1") as $result){
$gtTIme = $result['date'];
$time = strtotime($gtTIme);
$ftime = date("Y-m-d g:ia", $time);
$days_ago = date('Y-m-d g:ia', strtotime('-60 days'));
if ($ftime > $days_ago) {
$unl=$result['name'] . '<br />';
echo("</option><option value=". $uml . ">". $result['name'] ."</option>");
}
};
此代码不起作用:
foreach ($my_con->query("SELECT subject, date, table1.name, score, studentname FROM table1 JOIN table2 ON table1.name = table2.studentname ORDER BY table2.score DESC") as $result){
$gtname = $result['name'];
$gtsubject = $result['subject'];
$gtScore = $result['score'];
$gtTIme = $result['date'];
$time = strtotime($gtTIme);
$ftime = date("Y-m-d g:ia", $time);
$days_ago = date('Y-m-d g:ia', strtotime('-60 days'));
if ($ftime > $days_ago) {
$unl=$result['name'] . '<br />';
echo("</option><option value=". $uml . ">". $result['name']."</option>");
}
};
表1
name | subject | date
------+-----------+----------
Kyle | Science | 2014-04-03 4:00pm
John | Science | 2014-05-04 9:30am
Peter | Maths | 2013-10-20 9:30am
表2
score | studentname
-------+-------------
78 | John
89 | Kyle
83 | Peter
我希望回声是这样的:
Kyle Science 89
JOHN Science 78