假设此表:posts_groups

+---------+----------+--------+
| post_id | group_id | type   |
+---------+----------+--------+
| 1       | 1        | public |    // group #1 sees post #1 where type is public
+---------+----------+--------+

桌子:posts
+----+--------+
| id | type   |
+----+--------+
| 1  | public |  // example : post #1 is public
+----+--------+

查询1
SELECT post_id FROM posts_groups pg
JOIN posts p ON p.id = pg.post_id
WHERE group_id = 1 AND pg.type = public

好吧,这个检查组是1,并且从当前表中type,然后选择一些post\u id。
查询2
SELECT post_id FROM posts_groups pg
JOIN posts p ON p.id = pg.post_id
WHERE group_id = 1 AND p.type = public

此查询也在执行相同的操作。但是从POSTS表中检查type
但哪种方法更快,性能更好。你知道这两个查询是如何工作的吗。
条件处理!
在查询1中,它显示if(group_id=1,p.type=public),然后if(post.id=posts_groups.id)加入posts
在查询2中,它显示if(group_id=1)THEN if(post.id=posts_groups.id)THEN if(posts.type=public)JOIN posts。
query2的过程要长一些。或者我的逻辑是错误的!!!
提前谢谢。

最佳答案

两个查询没有区别,只要继续尝试使用您喜欢的查询。
编辑:当你编辑你的问题。
它们在WHERE子句之前连接,所以没有区别。从第一个或第二个表中获取条件,因为它们首先连接。

08-04 17:20