Skip to content
Advertisement

Incorrect syntax near the keyword ‘right’

i am very new to SQL, spent hours searching but all the articles point to more or less same

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;  

Bellow is my code, but honestly not sure what am i doing wrong here.

  "SELECT * FROM user_private_store WHERE store_type=@STORE_TYPE " +
     "RIGHT JOIN user_private_store_items " +
     "ON user_private_store.id = user_private_store_items.id";

I want to join both tables via ID, keep the id from left and take other parameters from right table user_private_store_items

i get Incorrect syntax near the keyword ‘right’

Advertisement

Answer

Just put the where clause after the joins:

"SELECT * FROM user_private_store s " +
"RIGHT JOIN user_private_store_items i " +
"ON s.id = i.id " +
"WHERE store_type=@STORE_TYPE"

Notes:

  • table aliases make the query easier to read and understand. I modified you query to use them

  • you would need to prefix store_type in the where clause with the table it belongs to, to avoid ambiguity

  • avoir select *; better enumerate all the columns that you want to select

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement