April 6, 2012

Insert into table with multiple select statements in MY SQL

Question by Ashara Shrestha

I have the result from a query like:

+------------------+------------+
| meta_key         | meta_value |
+------------------+------------+
| Destination Name | Shivapuri  |
| Destination Date | 26/03/2012 |
+------------------+------------+

I am trying to write a select statement with the Column name as Destination Name and Destination Date whose respective values are Shivapuri and ’26/03/2012′. How is this possible to do with a query in MY SQL?

Answer by Starx

SELECT 
    (CASE WHEN meta_key = 'Destination Name' THEN meta_value END) as name,
    (CASE WHEN meta_key = 'Destination Date' THEN meta_value END) as date
FROM `yourtable`

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!