JOIN如何在MySQL中工作? [英] How does JOIN work in MySQL?

查看:177
本文介绍了JOIN如何在MySQL中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管问题标题与许多讨论重复,但我没有找到这个问题的答案:

Although the question title is duplicate of many discussions, I did not find a answer to this question:

考虑一个简单的标签连接, p>

Consider a simple join for normalized tables of tags as

SELECT tags.tag
FROM tags
    INNER JOIN tag_map
    ON tags.tag_id=tag_map.tag_id
WHERE article_id=xx

JOIN 使用标签和tag_map的整个表,然后过滤创建的( JOIN ed)表以查找 WHERE 条款for article id

Does JOIN work with the entire tables of tags and tag_map then filter the created (JOINed) table to find rows with WHERE clause for the article id

JOIN 只会连接其中article_id = xx的tag_map表的行?

OR JOIN will only join rows of tag_map table in which article_id=xx ?

后一种方法应该会更快!

The latter method should be quite faster!

推荐答案

做前者,根据我的知识,WHERE是在结果JOINed表上显式执行。 (免责声明:MySQL可能会优化这在某些情况下,我不知道)。

It will do the former, to my knowledge WHERE's are explicitly performed on the resulting JOINed table. (Disclaimer: MySQL may optimize this in some cases, I don't know).

要强制后面的行为和执行WHERE第一,你可以添加一个额外的过滤器到您的JOIN ON语句:

To force the latter behaviour and execute the WHERE first, you can add an extra filter to your JOIN ON statement:

SELECT tags.tag 
    FROM tags 
    INNER JOIN tag_map 
        ON tags.article_id=xx
        AND tags.tag_id=tag_map.tag_id 
    WHERE article_id=xx

这篇关于JOIN如何在MySQL中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆