mysql 自我引用,MySQL的。如何使用自我加入

I need to use Self Join on this table.

+------------+------+--------+

| Country | Rank | Year |

+------------+------+--------+

|France | 55 | 2000 |

+------------+------+--------+

|Canada | 30 | 2000 |

+------------+------+--------+

|Liberia | 59 | 2001 |

+------------+------+--------+

|Turkey | 78 | 2000 |

+------------+------+--------+

|Japan | 65 | 2003 |

+------------+------+--------+

|Romania | 107 | 2001 |

+------------+------+--------+

I need to use self join to get what countries has the same year as Turkey.

Display the Country and year only.

This is what I am trying to do.

SELECT DISTINCT a.Country, a.Year

FROM table1 AS a, table1 AS b

WHERE a.Year=b.Year and a.Country='Turkey';

^ googled self join, and made it.

I am getting only Turkey. What am I doing wrong?

解决方案

You're so close!

Since you say you're displaying the country and year from A and limiting by A. Country of Turkey, Turkey is all you're going to see. You either need to change the selects to be B.country and B.year or change the where clause to be B.country.

This is using a cross join which will get slower the more records there are in a table.

SELECT DISTINCT b.Country, b.Year

FROM table1 AS a,

table1 AS b

WHERE a.Year=b.Year

and a.Country='Turkey';

could be written as... and would likely have the same execution plan.

SELECT DISTINCT b.Country, b.Year

FROM table1 AS a

CROSS JOIN table1 AS b

WHERE a.Year=b.Year

and a.Country='Turkey';

OR

This uses an INNER JOIN which limits the work the engine must do and doesn't suffer from performance degradation that a cross join would.

SELECT DISTINCT a.Country, a.Year

FROM table1 AS a

INNER JOIN table1 AS b

on a.Year=b.Year

and b.Country='Turkey';

WHY:

Consider what the SQL engine will do when the join occurs

A B

+------------+------+--------+------------+------+--------+

| A.Country | Rank | Year | B.Country | Rank | Year |

+------------+------+--------+------------+------+--------+

|France | 55 | 2000 |France | 55 | 2000 |

+------------+------+--------+------------+------+--------+

|Canada | 30 | 2000 |France | 55 | 2000 |

+------------+------+--------+------------+------+--------+

|Turkey | 78 | 2000 |France | 55 | 2000 |

+------------+------+--------+------------+------+--------+

|France | 55 | 2000 |Canada | 30 | 2000 |

+------------+------+--------+------------+------+--------+

|Canada | 30 | 2000 |Canada | 30 | 2000 |

+------------+------+--------+------------+------+--------+

|Turkey | 78 | 2000 |Canada | 30 | 2000 |

+------------+------+--------+------------+------+--------+

|France | 55 | 2000 |Turkey | 78 | 2000 |

+------------+------+--------+------------+------+--------+

|Canada | 30 | 2000 |Turkey | 78 | 2000 |

+------------+------+--------+------------+------+--------+

|Turkey | 78 | 2000 |Turkey | 78 | 2000 |

+------------+------+--------+------------+------+--------+

So when you said display A.Country and A.Year where A.Country is Turkey, you can see all it can return is Turkey (due to the distinct only 1 record)

But if you do B.Country is Turkey and display A.Country, you'll get France, Canada and Turkey!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值