question

tom 4 avatar image
tom 4 asked

ERROR #1064 --WHEN TRYING TO RUN A QUERY WITH JOINS

MY QUERY IS AS FOLLOWS:

SELECT C.Customer_Forename, Customer_Surname ,  j.Time , date, s.Staff_Forename , Staff_Surname v.Vehicle_ID, b.Status
From Booking b
INNER JOIN Vehicle v on v.Staff_ID = s.Staff_ID
INNER JOIN Journey j on j.Vehicle_ID = v.Vehicle_ID
INNER JOIN Booking b on b.Journey_ID = j.Journey_ID
INNER JOIN Booking b on b.Customer_ID = c.Customer_ID
Where b.Place_Number = "2000LT20080312"

ERROR MESSAGE IS AS FOLLOWS:

'#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '.Vehicle_ID, b.Status From Booking b INNER JOIN Vehicle v o

There is a chance that you may have found a bug in the SQL parser. Please examine your query closely, and check that the quotes are correct and not mis-matched. Other possible failure causes may be that you are uploading a file with binary outside of a quoted text area. You can also try your query on the MySQL command line interface. The MySQL server error output below, if there is any, may also help you in diagnosing the problem. If you still have problems or if the parser fails where the command line interface succeeds, please reduce your SQL query input to the single query that causes problems, and submit a bug report with the data chunk in the CUT section below: ----BEGIN CUT---- eNp9ksFuozAURff+irvoElwDmRmC1EWGui0VJSmm6TKC1G1IATMQovYzyswH1ySdDKuRLMvvvXvu lZ7M43gee/At+DZCHnmYwmJwpg4RiR70hPhL4eEsePLQ/irqtGllQ4s8o/WmNvawqe1Qm1qwGZuc W9a5phnzJvq4KGQ5BX+rcUbu3sV96GFCGbWZWagX8iBizIWB2TWPEgNLruMe8woBxw/KyOJu5mn3 75SZdeGQxc1ikBhzMZg41EWYV90bCWfRtQdZmXmrTNf9NjUtckgSPOR+Ap/2ftfuVCmb1ZVqZJWW sjfwcWqKrjn0YABb2if58d0/pbtB2NJe7NLn53+wHn4cWyd0T5dyk68LuQouDWRUj3ddS8hVo0r8 VOo1r16QkSCKeIzbeRDhS489VKXxo19wiQsd+LcY62+VzpLv2A767ShOE+P0MXMKHpiMflkcme2o /A9z2tIBWo9rQh43spFatCjStVxFXZnJRqt+65/AwkTfLnMs+88nA164/Q== ----END CUT---- ----BEGIN RAW----

ERROR: C1 C2 LEN: 9 10 393 STR: ’

CVS: $Id: sqlparser.lib.php,v 2.23.2.1 2004/11/10 00:40:48 lem9 Exp $ MySQL: 4.0.20-log USR OS, AGENT, VER: Win IE 7.0 PMA: 2.6.0-pl3 PHP VER,OS: 4.3.8 Linux LANG: en-iso-8859-1 SQL: SELECT C.’Customer_Forename’, ‘Customer_Surname’ , j.’Time’ , ’date’, s.’Staff_Forename’ , ‘Staff_Surname’ v.Vehicle_ID, b.Status

From Booking b INNER JOIN Vehicle v on v.Staff_ID = s.Staff_ID INNER JOIN Journey j on j.Vehicle_ID = v.Vehicle_ID INNER JOIN Booking b on b.Journey_ID = j.Journey_ID INNER JOIN Booking b on b.Customer_ID = c.Customer_ID

Where b.Place_Number = “2000LT20080312”

querymysql
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
This is MySQL, not SQL Server. There may be people here that know this information, but you might be better served by going to a MySQL discussion group.
3 Likes 3 ·
CirqueDeSQLeil avatar image CirqueDeSQLeil commented ·
This may be MySQL - however, i think the main problem resides in the alias of the inner join. That is something that can be helped with here. As for the mysql specific errors, I agree on finding a MySQL resource.
0 Likes 0 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
Please note that I have merged your other question with this one.
0 Likes 0 ·
Fatherjack avatar image
Fatherjack answered

This is mySQL but it looks like you should change the second join to the booking table with an AND

I think it should read

SELECT C.’Customer_Forename’, ‘Customer_Surname’ ,  j.’Time’ , ’date’, s.’Staff_Forename’ , ‘Staff_Surname’ , v.Vehicle_ID, b.Status
From Booking b
INNER JOIN Vehicle v on v.Staff_ID = s.Staff_ID
INNER JOIN Journey j on j.Vehicle_ID = v.Vehicle_ID 
    AND b.Journey_ID = j.Journey_ID
INNER JOIN Customer c on b.Customer_ID = c.Customer_ID    
Where b.Place_Number = “2000LT20080312”
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ian Roke avatar image Ian Roke commented ·
+1 Damn you are too quick for me! :-D
0 Likes 0 ·
CirqueDeSQLeil avatar image CirqueDeSQLeil commented ·
upvoting this one though there are two viable solutions. I prefer to combine the joins unless there is a good reason to separate them.
0 Likes 0 ·
Ian Roke avatar image
Ian Roke answered

From a SQL perspective you have this...

INNER JOIN Booking b on b.Journey_ID = j.Journey_ID
INNER JOIN Booking b on b.Customer_ID = c.Customer_ID

Wouldn't it be better just to do this?

INNER JOIN Booking b on b.Journey_ID = j.Journey_ID
AND b.Customer_ID = c.Customer_ID
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gianluca Sartori avatar image
Gianluca Sartori answered

You have used the alias 'B' three times for the table "Booking". Use another alias:

SELECT  C.Customer_Forename,
        Customer_Surname,
        j.Time,
        date,
        s.Staff_Forename,
        Staff_Surname,
        v.Vehicle_ID,
        <one of the Bookings alias>.Status
From    Booking b
INNER JOIN Vehicle v
        on v.Staff_ID = s.Staff_ID
INNER JOIN Journey j
        on j.Vehicle_ID = v.Vehicle_ID
INNER JOIN Booking B1
        on B1.Journey_ID = j.Journey_ID
INNER JOIN Booking b
        on B2.Customer_ID = c.Customer_ID
Where   <one of the Bookings alias>.Place_Number = '2000LT20080312'
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

CirqueDeSQLeil avatar image
CirqueDeSQLeil answered

I am not submitting a script here because Gianluca and FatherJack have already done that.

I am offering a little clarification.

You have

INNER JOIN Booking b on b.Journey_ID = j.Journey_ID
INNER JOIN Booking b on b.Customer_ID = c.Customer_ID

Both of these joins are using the same alias, which is not permissible in SQL.

Thus changing the alias on the second, of the two joins referenced, will allow the query to complete successfully. Fatherjack implemented an alternative solution which combined both of your join statements, whilst Gianluca corrected the second alias issue and left the joins in tact.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.