【问题标题】:Correct Syntax for MySQL Left joinMySQL左连接的正确语法
【发布时间】:2013-08-20 06:28:15
【问题描述】:

我是 inner joinleft join 的新手,希望有人能帮助我。 .

我有 3 张桌子..

  1. 衬衫
  2. 颜色
  3. 价格

衬衫:

sid  |姓名
------------
01 | |样品1
02 | |样本2

颜色

sid |颜色|| color_id
------------
01 |红色   | 900
02 |绿色 | 090

价格:

sid   |价格
------------
01 | 100
02 | 100

我的查询是:

SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
INNER JOIN `Colors`
on `Shirts`.`sid` = `Colors`.`sid`
LEFT JOIN `Prices`
on `Shirts`.`sid` = `Prices`.`sid`
WHERE `Shirts`.`sid`='02'

我想要实现的是:

sid |姓名    |颜色 |价格
----------------------
02 |样品 2 |绿色 | 100

我得到的是:

sid |姓名    |颜色 |价格
----------------------
空 |空的     |空 |空

我知道我的查询一定有问题..那么谁能告诉我什么是正确的查询?

【问题讨论】:

  • 问题可以在列GENDB_ID 中吗?必须有 Prices.SID 列吗?
  • 您的查询运行良好。我得到了正确的结果,和你预期的一样。

标签: mysql


【解决方案1】:

我认为你必须使用:

SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
INNER JOIN `Colors`
on `Shirts`.`sid` = `Colors`.`sid`
LEFT JOIN `Prices`
on `Shirts`.`sid` = `Prices`.`sid`
WHERE `Shirts`.`sid`='02'

Prices.GENDB_ID 不是有效字段或不是您必须使用的字段(我想)。

【讨论】:

  • sid的类型是什么?它们都是整数还是文本或其他?
【解决方案2】:

试试下面的。这部分

`on `Shirts`.`sid` = `Prices`.`GENDB_ID

看起来不正确。试试吧:

on `Shirts`.`sid` = `Prices`.`sid`

【讨论】:

    【解决方案3】:
    SELECT 
        `Shirts`.*, 
        `Colors`.`color`,
        `Prices`.`price` 
    FROM 
        `Shirts` INNER JOIN `Colors` on `Shirts`.`sid` = `Colors`.`sid`
         JOIN `Prices` on `Shirts`.`sid` = `Prices`.`sid` 
    WHERE `Shirts`.`sid`='02'
    

    【讨论】:

      【解决方案4】:

      试试这个:-

      SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
      INNER JOIN `Colors`
      on `Shirts`.`sid` = `Colors`.`sid`
      LEFT JOIN `Prices`
      on `Shirts`.`sid` = `Prices`.`sid` AND  `Prices`.`price`= 100   //Change this part.
      WHERE `Shirts`.`sid`='02'
      

      【讨论】:

        【解决方案5】:

        由于您是内连接和左连接的新手,我会尽快让您复习。

        首先,将您的查询更改为如下所示(您对参与 ON 过滤器的字段有误)

        SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
        INNER JOIN `Colors`
        on `Shirts`.`sid` = `Colors`.`sid`
        LEFT JOIN `Prices`
        on `Shirts`.`sid` = `Prices`.`sid`     //Change this.
        WHERE `Shirts`.`sid`='02'
        

        除此之外,当您使用left join 时,将哪些列放入where 条件(左表或右表中的列)确实很重要。

        要获得正确和预期的结果,如果您必须按 RIGHT 表中的列(在您的情况下为 Prices 表)过滤查询,则将该条件放入 ON 过滤器中,而不是 WHERE 过滤器中。

        例子:

        SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
        INNER JOIN `Colors`
        on `Shirts`.`sid` = `Colors`.`sid`
        LEFT JOIN `Prices`
        on `Shirts`.`sid` = `Prices`.`sid`     
        WHERE `Prices`.`price`= 100             //This can be wrong
        

        更正:

        SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
        INNER JOIN `Colors`
        on `Shirts`.`sid` = `Colors`.`sid`
        LEFT JOIN `Prices`
        on `Shirts`.`sid` = `Prices`.`sid` AND  `Prices`.`price`= 100   //Correct.
        //WHERE `Shirts`.`sid`='02'
        

        【讨论】:

          【解决方案6】:

          这应该可行:

          SELECT `Shirts`.*, `Colors`.`color`, `Prices`.`price` FROM `Shirts`
          INNER JOIN `Colors`
          on `Shirts`.`sid` = `Colors`.`sid`
          LEFT JOIN `Prices`
          on `Shirts`.`sid` = `Prices`.`sid`
          WHERE `Shirts`.`sid`='02'
          

          您获得全空行的原因是您在联接中包含GENDB_ID

          INNER JOINLEFT JOIN 之间的区别在于INNER JOIN 将排除两个表之间不会导致匹配的任何连接组合,而LEFT JOIN 将强制一行即使没有匹配.

          一个例子:

          衬衫

          sid  |   name 
          ------------ 
          01  | Sample1 
          02  | Sample2
          

          颜色

          sid |  color | color_id
          ------------ 
          01 |    red | 900
          03 |  green | 090
          

          内部联接

          SELECT * FROM Shirts INNER JOIN Colors ON Shirts.sid = Colors.sid
          
          sid |   name  | color | color_id
          ---------------------------------
          01  | Sample1 |  red  | 900
          

          左连接

          SELECT * FROM Shirts LEFT JOIN Colors ON Shirts.sid = Colors.sid
          
          sid |   name  | color | color_id
          ---------------------------------
          01  | Sample1 |  red  | 900
          02  | Sample2 |  null | null
          

          因此,在您的示例中,您强制使用LEFT JOIN 匹配不存在条件GENDB_ID 的行,这会导致行中所有列的值都只有空值。

          This previous post 也可能有帮助。

          希望这会有所帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-13
            相关资源
            最近更新 更多