当前位置:首页 > 开发 > 数据库 > 正文

MySQL: Left Join for More Than Two tables

发表于: 2013-04-19   作者:DavyJones2010   来源:转载   浏览次数:
摘要: 1. Tables used # 1. goods select * from goods; +----------+--------+------------+----------+ | goods_id | cat_id | goods_name | owner_id | +----------+--------+------------+----------+ |

1. Tables used

# 1. goods
select * from goods;
+----------+--------+------------+----------+
| goods_id | cat_id | goods_name | owner_id |
+----------+--------+------------+----------+
|        1 |      1 | CDMA Phone |        1 |
|        2 |      1 | GSM Phone  |        1 |
|        3 |      1 | 3G Phone   |        2 |
|        4 |      3 | TP Phone   |        1 |
+----------+--------+------------+----------+

# 2. cat
select * from cat;
+--------+---------------+
| cat_id | cat_name      |
+--------+---------------+
|      1 | Mobile Phone  |
|      2 | Settled Phone |
+--------+---------------+

# 3. owner
select * from owner;
+----------+------------+
| owner_id | owner_name |
+----------+------------+
|        1 | Davy       |
|        2 | Caly       |
|        3 | Jack       |
|        4 | Rose       |
+----------+------------+

 

2. How to join those three tables together?

 

#Possible attempt 1
select temp.*, owner.* from (select goods.*, cat.* from goods left join cat on goods.cat_id = cat.cat_id) as temp left join owner on temp.owner_id = owner.owner_id;
ERROR 1060 : Duplicate column name 'cat_id'

#Possible attempt 2
select temp.*, owner.* from (select goods.cat_id, goods.goods_name, goods.goods_id, cat.cat_name from goods left join cat on goods.cat_id = cat.cat_id) as temp left join owner on temp.owner_id = owner.owner_id;
ERROR 1054 : Unknown column 'temp.owner_id' in 'on clause'

#Possible attempt 3
select goods.cat_id, cat.cat_name, goods.goods_name, owner.owner_id, owner.owner_name from goods left join cat on goods.cat_id = cat.cat_id left join owner on goods.owner_id = owner.owner_id;
+--------+--------------+------------+----------+------------+
| cat_id | cat_name     | goods_name | owner_id | owner_name |
+--------+--------------+------------+----------+------------+
|      1 | Mobile Phone | CDMA Phone |        1 | Davy       |
|      1 | Mobile Phone | GSM Phone  |        1 | Davy       |
|      1 | Mobile Phone | 3G Phone   |        2 | Caly       |
|      3 | NULL         | TP Phone   |        1 | Davy       |
+--------+--------------+------------+----------+------------+

#Bingo!

MySQL: Left Join for More Than Two tables

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
mysql默认的join是inner join,inner join 和 left join的区别看图:
SQL: select * from t_a a left join t_b b on a.bid = b.bid left join t_c c on a.cid = c.cid t_
作者:zccst 一、 理论 (一)内连接(INNER JOIN) 内连接查询操作列出与连接条件匹配的数据行,它使用
看到某某用了左连接,右连接之类的,但是概念很模糊了 今天特地弄了一下 简单理解如下: 左连接(左
测试表用户表user: 测试表密码表pwd: 1、创建user表: CREATE TABLE `user` ( `id` int(11) NOT N
今天偶然间在stackoverflow看到此图,主要讲解mysql left join,right join,inner join和outer joi
最近做工程时,需要用一条SQL查询一张表中不同级别的字段(字段名称相同)。 直接上例子吧 如下是地点
作为开发,你是否经常碰到下面需要转换用户ID成用户名称的情况: 可惜你的这些业务表出于最少冗余设
<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View&
CREATE TABLE `a` ( `a1` VARCHAR(10) DEFAULT NULL, `b1` VARCHAR(10) DEFAULT NULL, `c1` VARCHAR
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号