gpt4 book ai didi

mysql - 如果不存在,我想将 `amount` 设置为 `-1`,下一步与 `-1` 进行比较

转载 作者:搜寻专家 更新时间:2023-10-30 23:14:56 26 4
gpt4 key购买 nike

我有这些记录:

Order
----------
id amount
1 10
2 NULL
3 20

如果不存在,我想将 amount 设置为 -1,下一步与 -1 进行比较。

我这样写:

SELECT 
id,
COALESCE(o.amount, -1) AS amount2
FROM Order o
WHERE amount2 = -1

但是返回这个错误:

#1054 - Unknown column 'amount2' in 'where clause'

我想返回:

Order
----------
id amount2
2 -1

最佳答案

您不能在 WHERE 子句中使用列别名。

试试这个:

SELECT 
id,
COALESCE(o.amount, -1) AS amount2
FROM Order o
WHERE COALESCE(o.amount, -1) = -1

An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column

引自此处:http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

关于mysql - 如果不存在,我想将 `amount` 设置为 `-1`,下一步与 `-1` 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15595280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com