简体   繁体   English

关于Where子句更好或ON子句的书写条件?

[英]Writing conditions on Where clause is better or ON clause?

Which query is standard and optimal? 哪个查询是标准查询和最佳查询?

This: 这个:

SELECT p.*
FROM posts p
JOIN favorites f ON p.id = f.post_id
WHERE f.user_id = ? 

Or this: 或这个:

SELECT p.*
FROM posts p
JOIN favorites f ON p.id = f.post_id
 AND f.user_id = ? 

Both query have written in standard way, it will work in almost all DBMS . 两种查询均以标准方式编写,几乎可以在所有DBMS中使用 But, if you concerned about the performance then i would use EXISTS instead. 但是,如果您担心性能,那么我会改用EXISTS

select p.*
from posts p
where exists (select 1 
              from favorites f 
              where p.id = f.post_id and f.user_id = ?
             );

For me doing filter in ON clause or in WHERE clause just the metter of style how it looks for INNER JOIN . 对我来说,在ON子句或WHERE子句中执行过滤器,只是样式看起来如何查找INNER JOIN So, i would go with WHERE clause : 因此,我将使用WHERE子句:

SELECT p.*
FROM posts p INNER JOIN 
     favorites f 
     ON p.id = f.post_id
WHERE f.user_id = ? 
ORDER BY f.date_time DESC;

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

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