With my SQL query, I want to counter up all the distinct values in a certain column. This works fine:
SELECT ans, COUNT(*) AS ans_num
FROM table_123
GROUP BY ans;
This returns (depending on how many distinct values) something like this, if there were three distinct values in the table:
+------+---------+
| ans | ans_num |
+------+---------+
| 0 | 15 |
| 1 | 2 |
| 3 | 11 |
+------+---------+
I'm using mysqli_fetch_array to return the query values in PHP, but I would have to loop through each row in order to get all the results.
QUESTION:
How can I write an SQL query that would return all the distinct values in one array (associative), so I would only have to mysqli_fetch_array only once?
CLARIFICATION:
I was wondering if there's a way in the query to return the results of the query as a single row, not fetching all rows with PHP
[[["ans"=>0], ["ans_num" => 15]], [["ans"=>1], ["ans_num" => 2]], [["ans"=>3], ["ans_num" => 11]]]...SELECT DISTINCT ans, COUNT(*) AS ans_num FROM table_123 GROUP BY category;USINGmysqli_fetch_all