gpt4 book ai didi

mysql - 试图将 avg 转换为小数,语法错误

转载 作者:行者123 更新时间:2023-11-30 22:17:09 25 4
gpt4 key购买 nike

我正在尝试将我的平均值转换为小数,但出现语法错误:

1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INT, avg(item_price)) as total_price, count(basket_item) a' at line 4 in

我的数据库:

SELECT 
item_id,
item_name,
CONVERT(INT, avg(item_price)) as total_price,
count(basket_item) as quantity,
item_price
FROM basket b, items i
WHERE b.basket_user = ?
AND i.item_id = b.basket_item
AND i.item_quantity > 0

我也尝试过转换,但遇到了同样的错误,我使用的是 xampp php 7 版本

最佳答案

avg 返回一个 DECIMAL。下面的代码应该可以解决这个问题。

More information on casting

SELECT CAST(2.234 AS UNSIGNED) FROM DUAL;

SELECT 
item_id,
item_name,
CAST(avg(item_price) AS UNSIGNED) as total_price,
count(basket_item) as quantity,
item_price
FROM basket b, items i
WHERE b.basket_user = ?
AND i.item_id = b.basket_item
AND i.item_quantity > 0

关于mysql - 试图将 avg 转换为小数,语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37908581/

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