Steberz Steberz - 1 month ago 11
MySQL Question

I cannot find the error in my SQL Code?

So I am trying to list the number and name of each customer that either is represented by sales representative 65 or that currently has orders on file, or both, and I keep getting an error. I even checked the solution in the book, and my code looks the same. What is this error telling me? I attached the solution the book gives as well. Solution

mysql> SELECT CUSTOMER_NUM, CUSTOMER_NAME
-> FROM CUSTOMER
-> WHERE REP_NUM+'65'
-> UNION
-> SELECT C.CUSTOMER_NUM, CUSTOMER_NAME
-> FROM CUSTOMER C, ORDERS 0
-> WHERE C.CUSTOMER_NUM=0.CUSTOMER_NUM;
ERROR 1064 (42000): 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 '0
WHERE C.CUSTOMER_NUM=0.CUSTOMER_NUM' at line 6

Answer

You have zero as alias name that is the reason for error. Numbers are not allowed to be aliased. I think you o instead of zero

SELECT CUSTOMER_NUM, CUSTOMER_NAME
     FROM CUSTOMER
     WHERE REP_NUM = '65'  -- also here `=` should be used instead of `+`
     UNION
     SELECT C.CUSTOMER_NUM, CUSTOMER_NAME
     FROM CUSTOMER C, ORDERS o
     WHERE C.CUSTOMER_NUM=o.CUSTOMER_NUM;