So I have a table with product sales.
Table:
- id
- name
- price
- amount
- date
I am trying to grab the average price of the items per day. If I run both my queries seperarely they work, I've been trying to figure this out all day and I cant seem to figure it out. So here's my query.
SELECT id, date, name, AVG(price) as today FROM sales
WHERE DATE(FROM_UNIXTIME(date)) = (CURRENT_DATE()) AND id = 1 GROUP BY id
UNION
SELECT id, date, name, AVG(price) as yesterday FROM sales
WHERE DATE(FROM_UNIXTIme(date)) = (CURRENT_DATE() - INTERVAL 1 DAY) AND id = 1 GROUP BY id) a group by id
I need it to be displayed as ,
[{"id":1,"name":"test","today":200, "yesterday": 100}]
I can get it so it displays both but for some reason it would show
[{"id":1,"date":1645881110,"name":"test","today":200},{"id":1,"date":1645881110,"name":"test","today":100}]
Which not sure why it displays "today" for both even tho its grabbing the correct data for the days.
If anyone can point me in the right direction that would be awesome and greatly appreciated.