0

In my DB I have 2 Tables. One for user Login information and one for general information.

Im trying to write a query that will select the column "firstname" from rows where the FK "users_id" is the same as the logged in users ID.

Before doing anything in PHP Im running the query in my database, so the logged in users ID, which would normally be a variable, is replaced with the id of my testuser.

This is my query:

SELECT b6vjp_user_info.firstname 
          FROM b6vjp_user_info 
          WHERE b6vjp_user_info.users_id LIKE 243
          INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id;

And here is my (censored for security reasons) Login Table named "b6vjp_users":

enter image description here

And here is my other table named "b6vjp_user_info":

enter image description here

The error is:

#1064 - Mistake in SQL-Syntax. 'INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id LIMIT 0, 25' on row 4

Now fyi I translated that, because my work environment is in german. But im sure you know what a Syntax-Error is.

Anyways I checked the JOIN Part of my query over and over again and looked up the JOIN tutorial on W3Schools. But there is no apparent mistake.

Does anybody see what I somehow fail to?

2
  • 4
    Put the WHERE clause after the (last) ON clause. Commented Sep 6, 2018 at 10:58
  • @jarlh Oh god such a simple mistake. Do you want to do the answer so I can mark it as solved? Need to wait 3 days when doing my own answers. Thanks! Commented Sep 6, 2018 at 11:00

3 Answers 3

1

Put the WHERE clause after the (last) ON clause.

Sign up to request clarification or add additional context in comments.

2 Comments

I can accept the answer in 12 Minutes :) Thanks!
You're welcome!
0

I strongly recommend using table aliases. You also need to fix the order of your SQL clauses. So:

SELECT ui.firstname 
FROM b6vjp_user_info ui JOIN
     b6vjp_users u
     ON ui.users_id = u.id
WHERE ui.users_id = '243';

Or, more simply without the JOIN:

SELECT ui.firstname 
FROM b6vjp_user_info ui 
WHERE ui.users_id = '243';

Notes:

  • The operand to LIKE should be a string. So, make it a string! Implicit type conversion causes all sorts of problems.
  • If you are not using wildcards, I think = is more informative. If users_id is really a number, then just use = 243 rather than LIKE.
  • WHERE goes after the FROM clause. JOIN is an operator in the FROM clause.
  • The JOIN is not necessary. Unless you are fetching columns from the users table (or need it for filtering which is highly doubtful), don't bother with it.

1 Comment

Wow thanks! This write up was very informative! Ill make sure to try integrating all of this into my coding :)
0

you have to put where after on clause

  SELECT b6vjp_user_info.firstname 
              FROM b6vjp_user_info                   
              INNER JOIN 
              b6vjp_users ON  
              b6vjp_user_info.users_id=b6vjp_users.id
              WHERE b6vjp_user_info.users_id =243 // i think it int field so no need to use like operator

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.