Home  >  Q&A  >  body text

Learn how to use inner joins and where clauses in MySQL

I have two independent tables, namely tuition and user. In the tuition table, I have two fields named user_id and admin_id. I use Inner Join clause with user_id to access related users:

SELECT * FROM tuition t JOIN user u ON t.user_id = u.id

But in the tuition table, I only want to join those records where admin_id is equal to 1. How should I rewrite the above clause? I wrote the following code, but it doesn't show any results:

SELECT * FROM tuition t JOIN user u ON t.user_id = u.id WHERE admin_id=1
SELECT * FROM tuition t WHERE admin_id=1 AND JOIN user u ON t.user_id = u.id

P粉496886646P粉496886646276 days ago331

reply all(1)I'll reply

  • P粉564192131

    P粉5641921312024-01-17 11:59:58

    The where clause of the inner join query should reference the table name.

    SELECT * FROM tuition t JOIN user u ON t.user_id = u.id WHERE table_name.admin_id=1

    reply
    0
  • Cancelreply