SQL语法错误排查请求:多表查询WHERE子句报1064错误
Hey there! That 1064 error is a classic syntax red flag, and I can spot the exact issue right away. Let's walk through fixing it.
The Root Problem
In your WHERE clause, you're using commas to separate table join conditions (like destination.destination_id=flights.d_id, plane.plane_id=flights.p_id). MySQL doesn't accept commas as condition separators here—you need to use AND to link those equality checks properly.
Quick Fix (Sticking to Your Original Structure)
Here's the corrected query with commas replaced by AND in the WHERE clause:
SELECT `traveller`.`ticket_no`, `traveller`.`fname`, `traveller`.`lname`, `destination`.`d_name`, `plane`.`plane_name`, `flights`.`flight_name` FROM `traveller`, `destination`, `flights`, `plane`, `checked` WHERE `destination`.`destination_id` = `flights`.`d_id` AND `plane`.`plane_id` = `flights`.`p_id` AND `checked`.`flight_id` = `flights`.`flight_id` AND `checked`.`t_id` = `traveller`.`ticket_no` AND `traveller`.`ticket_no` = "1"
Better Practice: Use Explicit JOIN Syntax
While the quick fix works, using explicit JOIN clauses makes your query way more readable and avoids accidental cross joins. Here's the same logic rewritten with modern, standard JOIN syntax (I also added table aliases to clean things up):
SELECT t.ticket_no, t.fname, t.lname, d.d_name, p.plane_name, f.flight_name FROM `traveller` t JOIN `checked` c ON c.t_id = t.ticket_no JOIN `flights` f ON c.flight_id = f.flight_id JOIN `destination` d ON d.destination_id = f.d_id JOIN `plane` p ON p.plane_id = f.p_id WHERE t.ticket_no = "1"
Either version should resolve that 1064 error for you. Let me know if you hit any other snags!
内容的提问来源于stack exchange,提问作者Anubis

