Vince V.
Vince V.

Reputation: 3143

Mysql Group by column names

I've written a query where I select the likes and the dislikes out of a column. I get the results but now I want to add a name to each of the grouped result, is this possible ?

Table structure:

id , snippet_id , user_id , kind

the query:

SELECT COUNT(v) as likes FROM Vote v GROUP BY v.kind

The result I get:

array(0 => 10, 1 => 3);

I want a result like this:

array('likes' => 10, 'dislikes' => 3);

Upvotes: 0

Views: 2758

Answers (3)

Daniel Pereira
Daniel Pereira

Reputation: 1785

You can return an associative array using mysql_fetch_assoc.

Upvotes: 0

Sebastien
Sebastien

Reputation: 6660

I think you use

mysql_fetch_array

to get your results, you have to use

 mysql_fetch_assoc

I hope to help you.

Upvotes: 0

a1ex07
a1ex07

Reputation: 37364

Add v.kind to your field list:
SELECT v.kind, COUNT(v) as likes FROM Vote v GROUP BY v.kind

Upvotes: 5

Related Questions