简体   繁体   English

自然加入mysql 5.6

[英]Natural join mysql 5.6

I would like to see how many medals a country have won.我想看看一个国家赢得了多少奖牌。 So I decide to combine the tables www_result and www_country.所以我决定合并表 www_result 和 www_country。 They have the attribute country_id in common.它们具有共同的属性 country_id。 This is my code so far but I get syntax errors, and I can't seem to find the reason.到目前为止,这是我的代码,但出现语法错误,而且我似乎找不到原因。

SELECT * FROM www_results WHERE position = 1 OR position = 2 OR position = 3
NATURAL JOIN 
www_countries;

Thank you in advance先感谢您

You just have things in the wrong order, do the JOINs then apply the WHERE clause...您只是顺序错误,执行 JOIN 然后应用 WHERE 子句...

You should also specify which table you are referring to when referencing a field name.您还应该在引用字段名称时指定所引用的表。 It's not always necessary but makes it more readable and less prone to bugs.它并不总是必要的,但会使其更具可读性且不易出错。

SELECT
    *
FROM
    www_results
NATURAL JOIN 
    www_countries
WHERE
    www_results.position IN (1, 2, 3)
;

I also actually recommend against NATURAL JOIN any way.我实际上也建议不要以任何方式使用NATURAL JOIN If you incidentally have fields with the same name, you get unwanted behaviour.如果您偶然拥有同名的字段,则会出现不需要的行为。 I would use explicit INNER JOIN s.我会使用显式的INNER JOIN

SELECT
    *
FROM
    www_results
INNER JOIN 
    www_countries
        ON  www_results.country_id = www_countries.country_id
        --  Replace the "country_id" with whatever field you want to join on
WHERE
    www_results.position IN (1, 2, 3)
;

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

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