0
我一直在关注这个左连接的答案,但不能让我的工作左连接MySQL的
MYSQL Left Join last row of result
我有两个表
leicester
count area_id section toberth
1 LR WL 300
2 LR WL 301
3 ER SN 300
berthmove
count area_id timemove description toberth
1 LR 124 6R43 300
2 LR 126 5RT4 300
3 TR 128 6TF7 310
4 LR 121 6TT3 301
5 LR 122 5RR7 301
6 TR 127 7YY9 300
这里是我的代码
$stmt=$mysql_link->prepare("SELECT l.*,b.*
FROM leicester AS l
LEFT JOIN berthmove AS b ON l.toberth=b.toberth WHERE l.area_id=b.area_id AND l.section="WL"
LEFT JOIN berthmove AS b2 ON l.toberth=b2.toberth AND b.timemove<b2.timemove
WHERE b2.timemove is NULL LIMIT 1 ");
$stmt->execute();
我试图结束5RT4和5RR7的描述,即泊位表中的第2行和第5行。即从'leicester'表中选择泊位300和301(即它们在WL部分),然后在300和301上找到描述。但它可能只是最新的描述(基于timemove)。还需要检查area_id是否相同。
任何帮助,请
继承人的取
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $berths)
{
echo "hello";
$selectberth=$berths['toberth'];
$description=$berths['descr'];
echo $description;
echo $selectberth;
}
编辑
更新代码,以反映更改到WHERE
$stmt=$mysql_link->prepare("SELECT l.*,b.*
FROM leicester AS l
LEFT JOIN berthmove AS b ON l.toberth=b.toberth
LEFT JOIN berthmove AS b2 ON l.toberth=b2.toberth AND b.timemove<b2.timemove
WHERE l.area_id=b.area_id AND l.section='WL' AND b2.timemove is NULL LIMIT 1 ");
$stmt->execute();
的'where'去毕竟'join's。你只能在'select'中有1个'where'。另外'AND l.section =“WL”'应该会导致解析错误并破坏你的页面加载。使用错误报告。 – chris85
毕竟已经搬到了哪里。我收到解析错误。它说出乎意料的T_string。不知道是什么问题,但 – user2635961
你以前没有得到这个错误?阅读完整评论并更正报价。 – chris85