I'm trying to create a PHP script that will echo the average value of each of my columns and array them all in a single JSON array.
I know that this is how I can get the average for a column.
select avg(`sales`) as sales from `mytable`
But I'm not sure how to string this together
select avg(`sales`) as sales from `mytable`
select avg(`profit`) as profit from `mytable`
select avg(`costs`) as costs from `mytable`
To get something like this echo'd from PHP:
[
{
"sales": 56812
},
{
"profit": 2312
},
{
"costs": 324
}
]
json_encode()
- never form the JSON by hand. You could use aUNION
query orJOIN
to get all of the data in one shot.