1

I am trying to turn my avg value in to a decimal, but am getting a syntax error:

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

my sql:

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

I have tried cast too but am getting the same error, I am using xampp php 7 version

4334738290
  • 393
  • 2
  • 19

1 Answers1

0

avg is returning a DECIMAL. The code below should fix the problem.

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
Community
  • 1
  • 1
tomwj
  • 46
  • 1
  • 6