Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql sql不允许在where子句比较中使用case变量_Mysql_Sql_Sql Server - Fatal编程技术网

Mysql sql不允许在where子句比较中使用case变量

Mysql sql不允许在where子句比较中使用case变量,mysql,sql,sql-server,Mysql,Sql,Sql Server,我正在尝试在SQLServer中运行查询 我想加入2列并比较这两个列的数量&打印第1个表(sid)的数量少于第2个表(idt) 因为我需要第一个表的所有数据,而不考虑是否有联接,所以我使用左联接并将null字段设置为0 SELECT sid.id, sid.total_quantity, idt.id, CASE WHEN idt.total_quantity IS NULL THEN 0 ELSE idt.total_quantity END as total_quantity

我正在尝试在SQLServer中运行查询

我想加入2列并比较这两个列的数量&打印第1个表(sid)的数量少于第2个表(idt)

因为我需要第一个表的所有数据,而不考虑是否有联接,所以我使用左联接并将null字段设置为0

SELECT sid.id, sid.total_quantity, idt.id,
CASE 
    WHEN idt.total_quantity IS NULL THEN 0
   ELSE idt.total_quantity
END as total_quantity2
FROM abc_details as sid
LEFT JOIN (SELECT * FROM def_details WHERE location_name = 'XYZ') as idt
ON sid.item_no = idt.item_no 
WHERE sid.stock_invoice_id = 37
ORDER BY sid.item_code, sid.lot_number
这与案例条件的col_name“total_quantity2”配合得很好

但是,当我试着比较

WHERE sid.stock_invoice_id = 37 AND sid.total_quantity < total_quantity2

为什么会发生这种情况以及如何解决这一问题您可以将查询简化为:

SELECT sid.id, sid.total_quantity, idt.id,
       COALESCE(idt.total_quantity, 0) as total_quantity2
FROM abc_details sid LEFT JOIN
     def_details idt
     ON sid.item_no = idt.item_no AND
        idt.location_name = 'XYZ'
WHERE sid.stock_invoice_id = 37
ORDER BY sid.item_code, sid.lot_number;
这将
CASE
表达式更改为
COALESCE()
,并删除子查询

对于
WHERE
子句,您不妨重复以下表达式:

WHERE sid.stock_invoice_id = 37 AND
      sid.total_quantity < COALESCE(idt.total_quantity, 0)

也就是说,如果希望在
WHERE
子句中使用此不等式,则需要匹配的行。

您无法访问WHERE子句中的列别名。提示:
coalesce(idt.total_quantity,0)
而不是
case
。你是个天才。好办法
WHERE sid.stock_invoice_id = 37 AND
      sid.total_quantity < COALESCE(idt.total_quantity, 0)
SELECT sid.id, sid.total_quantity, idt.id,
       idt.total_quantity as total_quantity2
FROM abc_details sid JOIN
     def_details idt
     ON sid.item_no = idt.item_no AND
        idt.location_name = 'XYZ'
WHERE sid.stock_invoice_id = 37 AND
      sid.total_quantity < idt.total_quantity
ORDER BY sid.item_code, sid.lot_number;