0

I have a table like this:

id name 
1  a    
2  b    
3  c    
4  d   
5  e    

If I do select name from tableTest where id = 1 or id = 2, I got this:

name
a
b

But I want a result like this:

name_1   name_2 
a        b

-- 1, 2: Best: the value of id, or (AA, BB..) or (a, b, c,..) or anything

How can I do that

Mureinik
  • 277,661
  • 50
  • 283
  • 320
Sato
  • 7,432
  • 15
  • 54
  • 101
  • 1
    if you using php then you can do it in php or whatever langauge you use – echo_Me Apr 30 '14 at 05:27
  • the idea count how many data returned then create generated `name_i` then assign every value to its generated `name_i` – echo_Me Apr 30 '14 at 05:28
  • Doesn't help you sorry, but for any SQL Server people who find this, the [PIVOT and UNPIVOT](http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx) commands can do this. – Jeff Apr 30 '14 at 05:33
  • possible duplicate of [MySQL pivot row into dynamic number of columns](http://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns) – Jeff Apr 30 '14 at 05:35
  • Take a look at [this answer](http://stackoverflow.com/a/12005676/781965) to [another question about the same thing](http://stackoverflow.com/q/12004603/781965). – Jeff Apr 30 '14 at 05:37

1 Answers1

0
select max(case when id=1 then name end) as name_1,
        max(case when id=2 then name end) as name_2
From test

SQL Fiddle Demo

Mudassir Hasan
  • 26,910
  • 19
  • 95
  • 126