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