gpt4 book ai didi

mysql - 无法使用别名作为字段

转载 作者:行者123 更新时间:2023-11-29 12:43:52 24 4
gpt4 key购买 nike

考虑像这样开始查询:

SELECT

(SELECT ps2.price FROM product_special ps2
WHERE p.product_id = ps2.product_id
AND ((ps2.date_start = '0000-00-00' OR ps2.date_start < NOW())
AND (ps2.date_end = '0000-00-00' OR ps2.date_end > NOW()))
ORDER BY ps2.priority ASC, ps2.price ASC LIMIT 1) AS special,

-- here is the problem, i can't use subquery alias special as field that i can
-- calculate with

IF(tr.rate ,CEILING(special + (special * tr.rate / 100 )),special) AS final_price

-- ending not important

有没有办法可以在此查询中使用别名作为字段?我已经在这里遇到了一些类似的问题,但没有一个对我有帮助。

最佳答案

这是 MySQL 中 Sub Select 查询中的一个众所周知的错误,无法用于计算!您有一个解决方法:

SELECT

(SELECT ps2.price FROM product_special ps2
WHERE p.product_id = ps2.product_id
AND ((ps2.date_start = '0000-00-00' OR ps2.date_start < NOW())
AND (ps2.date_end = '0000-00-00' OR ps2.date_end > NOW()))
ORDER BY ps2.priority ASC, ps2.price ASC LIMIT 1) AS special,
IF(tr.rate ,CEILING((SELECT ps2.price FROM product_special ps2
WHERE p.product_id = ps2.product_id
AND ((ps2.date_start = '0000-00-00' OR ps2.date_start < NOW())
AND (ps2.date_end = '0000-00-00' OR ps2.date_end > NOW()))
ORDER BY ps2.priority ASC, ps2.price ASC LIMIT 1)+ ((SELECT ps2.price FROM product_special ps2
WHERE p.product_id = ps2.product_id
AND ((ps2.date_start = '0000-00-00' OR ps2.date_start < NOW())
AND (ps2.date_end = '0000-00-00' OR ps2.date_end > NOW()))
ORDER BY ps2.priority ASC, ps2.price ASC LIMIT 1)* tr.rate / 100 )),(SELECT ps2.price FROM product_special ps2
WHERE p.product_id = ps2.product_id
AND ((ps2.date_start = '0000-00-00' OR ps2.date_start < NOW())
AND (ps2.date_end = '0000-00-00' OR ps2.date_end > NOW()))
ORDER BY ps2.priority ASC, ps2.price ASC LIMIT 1)) AS final_price

我知道这可能很疯狂,但请检查它是否有效!

关于mysql - 无法使用别名作为字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25724702/

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