【问题标题】:Mysql Column cannot be null error when there is no result using LEFT JOIN使用 LEFT JOIN 没有结果时,Mysql Column cannot be null 错误
【发布时间】:2016-07-20 14:35:42
【问题描述】:

我在客户表中有一些数据,而其他表中没有数据。因此,当我尝试使用以下查询进行选择时,它给了我

1048-列 'customerid' 不能为空

。可能是什么问题呢。有解决办法吗?

SELECT c.*, fd.ruakzat as ruak,
        fd.khatzat as khat, 0+0 as belh, 0+0 as neih, 
        puk.lended as hawh
        FROM `customer` c
        LEFT JOIN ( 
                SELECT s.customerid, o.orderzat as ruakzat, 
                       f.filled as khatzat     
                FROM `sale` s    
                LEFT JOIN (     
                       SELECT SUM(quantity) as orderzat,invoiceno     
                       FROM `order`    
                       WHERE fillstatus='Empty'
                       GROUP BY invoiceno    
                 ) AS o ON s.invoiceno=o.invoiceno    
                LEFT JOIN (     
                      SELECT SUM(quantity) as filled,invoiceno    
                      FROM `order`    
                      WHERE fillstatus='Filled'    
                      GROUP BY invoiceno    
                ) AS f ON s.invoiceno=f.invoiceno    
        ) AS fd ON c.id=fd.customerid    
        LEFT JOIN (     
             SELECT SUM(quantity) as lended, customerid    
             FROM `lending`    
        ) AS puk ON c.id=puk.customerid     
       WHERE (puk.customerid IS NULL OR c.name LIKE '%%')

什么是更好的方法?感谢您的帮助。

【问题讨论】:

  • 看看c.id=fd.customerid。你说其他表中没有customerid。看起来 fd.customerid 来自 sale 表。
  • 是的,你是对的。但是该表中没有数据。但是 customerid 字段在那里。
  • 你在尝试INSERT INTO ... SELECT吗?
  • 有 5 个customerid 哪一个给你错误?分而治之。单独测试每个子查询并逐步构建您的查询。
  • 正如@Philipp 所说,该查询中没有任何内容会产生该错误。您可能还缺少其他东西。

标签: mysql sql subquery left-join


【解决方案1】:

MySQL 有一些关于类似问题的错误报告(https://bugs.mysql.com/bug.php?id=31450https://bugs.mysql.com/bug.php?id=35633https://bugs.mysql.com/bug.php?id=52441),因此您可能遇到了相同的错误。

总结一下:如果有问题的字段根据定义是不可为空的(在创建表中定义为不为空),但出现在输出结果可能为空的子查询中。

请检查您的 MySQL 版本,因为它可能是出现上述错误之一的版本。

此外,对我来说子查询

         SELECT SUM(quantity) as lended, customerid    
         FROM `lending`    

没有太大意义,因为没有按部分分组。这会将借贷表折叠成一条记录,其中 customerid 从其中一条记录中随机选择。所以,我会在上面的子查询中添加一个group by customerid

【讨论】:

  • 感谢您的回答。我也刚刚发现问题出在借贷表上。我添加了 BY 组,它可以工作。谢谢。
【解决方案2】:

与你的问题无关,但是你可以替换那两个加入

        LEFT JOIN (     
               SELECT SUM(quantity) as orderzat,invoiceno     
               FROM `order`    
               WHERE fillstatus='Empty'
               GROUP BY invoiceno    
         ) AS o ON s.invoiceno=o.invoiceno    
        LEFT JOIN (     
              SELECT SUM(quantity) as filled,invoiceno    
              FROM `order`    
              WHERE fillstatus='Filled'    
              GROUP BY invoiceno   

单用Conditional SUM

        LEFT JOIN (     
               SELECT invoiceno, 
                      SUM(CASE WHEN fillstatus='Empty' THEN quantity
                                                       ELSE 0
                          END) as orderzat,     
                      SUM(CASE WHEN fillstatus='Filled' THEN quantity
                                                        ELSE 0
                          END) as filled     
               FROM `order`    
               GROUP BY invoiceno    
         ) AS o ON s.invoiceno=o.invoiceno   

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 2022-10-18
    • 2022-11-04
    • 1970-01-01
    • 2016-12-16
    • 2019-03-25
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多