I have a table that has a column called type it holds one of two words upvote or downvote I want to group by a.question_id then get 2 columns with a count of each one.
select a.*, u.* from answers a
left join users u using(user_id)
left join vote_history v using(answer_id)
where a.question_id = 4 and deleted < 4
group by v.question_id
order by votes desc
Example opt
ColumnA | ColumnB | upvotes | downvotes
---------+----------+---------+----------
record1A | record2B | 3 | 2
record2A | record2B | 2 | 5
Is it possible for me to do this without doing more than one query, and without doing a sub query?
This is known as a pivot. In MySQL you can use an aggregate function with a
CASEexpression to transform the data into columns: