How to query in such a way that values becomes columns?
I think what you are looking for is called a Pivot.  I work primarily with Microsoft SQL Server, so I don't know all the inter-workings of this.

There appears to be a really well received Stack Overflow post that answers a similar question for MySQL.

https://stackoverflow.com/questions/7674786/mysql-pivot-table

Hopefully that give you what you are looking for.