gpt4 book ai didi

mysql - mysql别名的未知列问题

转载 作者:可可西里 更新时间:2023-11-01 06:31:43 25 4
gpt4 key购买 nike

当列是创建的别名时,我不明白为什么我会得到一个未知列。任何帮助都会很棒。

代码:

SELECT DISTINCT 
c.id,
((SUM(c.width_feet)*12)+(SUM(c.width_inches))) AS width,
((SUM(c.height_feet)*12)+(SUM(c.height_inches))) AS height
FROM carpets AS c
WHERE c.active = '1'
AND (width BETWEEN '0' AND '275')
AND (height BETWEEN '0' AND '599')
ORDER BY c.item_no

错误:

Unknown column 'width' in 'where clause'

最佳答案

您不能直接通过名称访问别名。

一种解决方案是在子查询中使用别名包装查询,然后在外部查询中引用别名:

SELECT DISTINCT * 
FROM
(
SELECT c.id,
((SUM(c.width_feet)*12)+(SUM(c.width_inches))) AS width,
((SUM(c.height_feet)*12)+(SUM(c.height_inches))) AS height
FROM carpets AS c
WHERE c.active = '1'
) sub
WHERE (sub.width BETWEEN '0' AND '275')
AND (sub.height BETWEEN '0' AND '599')
ORDER BY sub.item_no

关于mysql - mysql别名的未知列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9037122/

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