如何在MySQL中使用内连接与WHERE一起使用?

huangapple go评论63阅读模式
英文:

How to use Inner Join along with Where in MySQL?

问题

学费表中,我有两个字段分别是user_idadmin_id。我使用Inner Join子句和user_id访问相关的用户:

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

但在学费表中,我只想连接那些admin_id等于1的记录。我应该如何重新编写上面的子句?我尝试过以下方式但没有显示任何结果:

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
英文:

I have 2 separate tables which are tuition and user. In the tuition table I have 2 fields named user_id and admin_id. I access related users by using Inner Join clause and user_id:

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

But in the tuition table, I want join only records in tuition which their admin_id is equal to 1. How should I rewrite the clause above? I wrote as this but didn't show me anything:

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

答案1

得分: 1

Where clause with inner join queries should be referenced with table name.

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

英文:

Where clause with inner join queries should be referenced with table name.

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

huangapple
  • 本文由 发表于 2023年6月5日 02:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定