连接子表和父表 [英] Joining child and parent tables

查看:94
本文介绍了连接子表和父表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mysql的新手,我对使用不同的联接有一个快速的疑问.举例来说,我有2个表,一个表的标题是总计,另一个表的标题是商品,每个表都包含值,

I am a newbie in mysql and I have a quick question about using the different join. Say for instance, I have 2 tables, one titled totals and another is titled goods and each contains values,

ID | Name
--------
1 | coffee
2 | tea
3 | chocolate

总计

Quantity | goods ID
---------------------
40 | 3
20 | 2
10 | 1

例如,像这样,商品ID总计是商品表ID的子级,但是我想要的显示是

Like this for example, the goods ID in total is the child of the ID of the goods table, but the display I want is

Quantity | goods ID
-------------------
40 | chocolate
20 | tea
10 | coffee

我只想知道不同的联接在哪里适用.

I just want to know where the different joins would be applicable.

推荐答案

跟随Juergen发布的链接(该链接是非常好的参考),要获得第3个表的结果,您可以使用INNER JOIN,FULL OUTER联接或左/右外联接.这样做的原因是由于两个表的设置方式相互匹配,因此无需担心空值.

Following up from the link Juergen posted (that link is a very good reference), to achieve the result of the 3rd table you could use INNER JOIN, FULL OUTER JOIN or LEFT/RIGHT OUTER JOIN. The reason for this is because of the way your two tables are set up to match each other, hence there is no null values to worry about.

但是,如果表是这样设置的:

However, if the tables were set up like this:

ID |名称
--------
1 |咖啡
2 |茶
3 |巧克力
4 |牛奶

ID | Name
--------
1 | coffee
2 | tea
3 | chocolate
4 | milk

数量|商品编号
---------------------
40 | 3
20 | 2
10 | 1

Quantity | goods ID
---------------------
40 | 3
20 | 2
10 | 1

然后,要获得相同的结果,您需要使用INNER联接.

Then, to achieve the same result you would want to use an INNER join.

这篇关于连接子表和父表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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