简体   繁体   English

MySQL INNER JOIN 3个不同的表与WHERE

[英]MySQL INNER JOIN 3 different tables with WHERE

Sorry if this question might seem weird, but I am trying to INNER JOIN some tables in SQL and I can't make it right. 很抱歉,如果这个问题看起来很奇怪,但是我试图在SQL中INNER JOIN一些表,但我做对了。 I am new to this, so please if you care to explain what I'm doing wrong... i would really appreciate it. 我对此并不陌生,所以如果您愿意解释我在做什么错的话,我将不胜感激。

Basically, what I am trying to do is JOIN the following tables: meetings, users and service. 基本上,我想做的是加入下表:会议,用户和服务。 Users can be moderators or admins and that information is defined in the settings column. 用户可以是主持人或管理员,并且该信息在“设置”列中定义。 I want to INNER JOIN moderator_id with users.id WHERE users.settings = 'moderator' and also INNER JOIN admin_id with users.id WHERE users.settings = 'admin'. 我想与users.id WHERE users.settings ='moderator'一起INNER JOIN moderator_id,也要与users.id WHERE users.settings ='admin'一起INNER JOIN admin_id。

Hope you guys understand what I am trying to do :) 希望你们能理解我正在尝试做的事情:)

Thanks! 谢谢!

SELECT meetings.*
     , users.id
     , users.name
     , users.settings
     , services.id
     , services.service_name
     , services.price 
  FROM meetings 
  JOIN users 
    ON meetings.moderator_id = users.id 
 WHERE users.settings = 'moderator' 
  JOIN users 
    ON meetings.admin_id = users.id 
 WHERE users.settings = 'admin' 
  JOIN services 
    ON meetings.service_id = services.id 
 ORDER 
    BY meetings.date ASC   
SELECT meetings.*, users.id, users.name, users.settings, services.id, services.service_name, services.price 
FROM meetings

INNER JOIN users as mods ON meetings.moderator_id = mods.id 
INNER JOIN users as adms ON meetings.admin_id = adms.id
INNER JOIN services ON meetings.service_id = services.id

WHERE adms.users.settings = 'admin' AND mods.users.settings = 'moderator' 
ORDER BY appointments.date ASC

I guess you need to specify the users instance for admins and moderatores, then place the where after the joins. 我想您需要为管理员和主持人指定用户实例,然后将其放置在加入位置之后。 Try this query and tell me if it worked. 尝试此查询,并告诉我它是否有效。

The syntax for SELECT is: SELECT的语法为:

SELECT <fields>
FROM <table>
JOIN <table> ON <condition>
JOIN <table> ON <condition>
...
WHERE <condition> AND <condition> AND ...
ORDER BY <column>, <column>, ...

ie you list all tables involved followed by the conditions. 即,您列出了所有涉及的表以及条件。 That's the general structure for an SQL query so feel free to memorize it :). 这是SQL查询的一般结构,因此请随时记住:)。 Also please note that this is of course simplified in the extreme, there's much more to it. 还请注意,这当然是在极端情况下简化的,还有更多的事情要做。 (Eg OR instead of AND in the WHERE clause, but that's another story.) Good luck. (例如,在WHERE子句中使用OR代替AND,但这是另一个故事。)祝您好运。

Edit: If you alias a table, eg "FROM tablename AS newtablename" you should use the alias, newtablename, for conditions in WHERE, columns in SELECT, etc. Otherwise you get messages about things that can't be found. 编辑:如果您为一个表加上别名,例如“ FROM tablename AS newtablename”,则应为WHERE中的条件,SELECT中的列等使用别名,newtablename。否则,您会收到有关找不到的消息。 You sort of rename the table for the duration of the query and throw the old name away. 您可以在查询期间对表进行重命名,然后丢弃旧名称。

Eg: 例如:

SELECT m.*
     , mods.name mod_name
     , admins.name admin_name
     , s.service_name
     , s.price 
  FROM meetings m
  JOIN users mods
    ON mods.id = m.moderator_id 
  JOIN users admins
    ON admins.id = m.admin_id
  JOIN services s
    ON s.id = m.service_id 
 WHERE mods.settings = 'moderator' 
   AND admins.settings = 'admin';

... or, more likely... ...或更可能是...

SELECT m.*
     , mods.name mod_name
     , admins.name admin_name
     , s.service_name
     , s.price 
  FROM meetings m
  LEFT
  JOIN users mods
    ON mods.id = m.moderator_id 
   AND mods.settings = 'moderator' 
  LEFT
  JOIN users admins
    ON admins.id = m.admin_id
   AND admins.settings = 'admin'
  JOIN services s
    ON s.id = m.service_id;

I found the answer by combining some of your suggestions! 我通过结合您的一些建议找到了答案! It seems to be working just fine now, so thank you guys for your help! 现在看来一切正常,所以谢谢你们的帮助!

SELECT meetings.*, 
  mods.id, 
  mods.name as mods_name, 
  mods.settings, 
  admins.id, 
  admins.name as admins_name, 
  admins.settings, 
  services.id, 
  services.service_name, 
  services.price

FROM meetings 

INNER JOIN users as mods ON meetings.moderator_id = mods.id 
INNER JOIN users as admins ON meetings.admin_id = admins.id
INNER JOIN services ON meetings.service_id = services.id

WHERE mods.settings = 'moderator' AND admins.settings = 'admin' 
ORDER BY meetings.date ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM