I am trying to perform a count query in mysql to update a column (no_of_orders) in my users table using data from another table (orders).
The table I want to perform a query on is the orders table that has a created_date column in datetime format.
I would like to count the number of orders each user has made in the last 3 months.
I have tried the below and I am getting an error "Operand should only contain one column".
UPDATE users u SET no_of_orders = (
SELECT EXTRACT(month from created_date)>= NOW() - INTERVAL 3 MONTH ,
COUNT(*)
FROM orders o
WHERE o.created_by = u.user_id);
Is there a better way to do this?
Thank you!
GROUP BY. That won't solve your problem, but may help.where existsclause too.