gpt4 book ai didi

mysql - 带有计算值的 SQL WHERE 子句

转载 作者:可可西里 更新时间:2023-11-01 07:17:50 25 4
gpt4 key购买 nike

看来我不能在 WHERE 子句中使用计算值?

SELECT id, TIMESTAMPDIFF(YEAR,dateOfBirth,CURDATE()) AS age 
FROM user
WHERE age >= 20

我收到 ERROR 1054 (42S22): Unknown column 'age' in 'where clause'。一种可能的解决方法是使用子查询,但它过于复杂?

最佳答案

可以WHERE 子句中使用计算值,但不能使用其别名。你必须再次输入整个表达式

WHERE TIMESTAMPDIFF(YEAR,dateOfBirth,CURDATE()) >=20

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:

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

Reference

关于mysql - 带有计算值的 SQL WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32708178/

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