SELECT SUM(CAST(JSON_UNQUOTE(JSON_EXTRACT(order_details, '$.quantity')) AS UNSIGNED)) AS total_quantity
FROM orders;
JSON_EXTRACT(order_details, '$.quantity')
: Extracts the value of the quantity
key from the JSON order_details
column.JSON_UNQUOTE(...)
: Removes any quotes from the extracted JSON value.CAST(... AS UNSIGNED)
: Converts the extracted value to an unsigned integer to allow for summation.SUM(...)
: Sums all the quantities across the rows.