简体   繁体   English

MySQL:每个INNER JOIN的条件不同

[英]MySQL: Different conditions for each INNER JOIN

I have the following mysql query 我有以下mysql查询

SELECT n.* FROM notifications as n
  INNER JOIN notifications as nc ON ...  ...
     INNER JOIN following as f ON ...  ...
         WHERE
           (condition 1)
                OR
                  (condition 2)

My guestion is how i can make sure that condition 1 is evaluted for the first inner join (nc) and condition 2 is evaluted for the second inner join (f) 我的座右铭是如何确保对第一个内部联接(nc)评估条件1和对第二个内部联接(f)评估条件2

The best way is to move you conditions from WHERE clause to ON clause. 最好的方法是将您的条件从WHERE子句移到ON子句。 In case on Inner Join, WHERE and ON work in same manner and it is always better to filter out data as early as possibly in the query. 如果是内部联接,WHERE和ON的工作方式相同,最好尽早在查询中过滤掉数据。 So, your query will look like: SELECT n.* FROM notifications as n INNER JOIN notifications as nc ON ... ... AND (Condition 1) INNER JOIN following as f ON ... ... AND (Condition 2) 因此,您的查询将如下所示:SELECT n。* FROM通知为n INNER JOIN通知为nc ON ... ...(条件1)INNER JOIN随后为f ON ... ...(条件2)

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

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