0

I was wondering if it would be possible to get all values of rows with the same ID and present them as new columns, via a query.

For example, if I have the following table:

ID | VALUE
1  | a
1  | b
1  | c
2  | a
2  | b
[...]

I want to present it as:

ID | VALUE1 | VALUE2 | VALUE3 [...]
1  |      a |      b |      c 
2  |      a |      b |      - 

Thank you for any help

user2123665
  • 118
  • 7

1 Answers1

0

A query wouldn't do it. Unless you do 3 seperate querys.

SELECT ID,VALUE1 FROM Table 
SELECT ID,VALUE2 FROM Table 
ect...

If you have a problem with your database values not being recursive, then i would set up your table differently.

ID | VALUE 1 | a 1 | b 1 | c 2 | a 2 | b [...]

You should set up the Table atributes like that rather than your first table.

if you are going to set up your tables differently I would do insert Statements.

INSERT INTO newTable (ID, VALUE)
SELECT ID,VALUE1 FROM oldTable

INSERT INTO newTable (ID, VALUE)
SELECT ID,VALUE2 FROM oldTable

ect..

Another possible way to do it is to display it in your application. Take php for instance.

foreach($sqlArray as $var){
    echo $var['id'] ' | ' $var['value1']
    echo $var['id'] ' | ' $var['value2']
    echo $var['id'] ' | ' $var['value3']
}
Roger
  • 3,226
  • 1
  • 22
  • 38
  • I can code it differently say,using java or php, but that's the result of a Join Query on 2 different tables of the database. Just wondering if that would be possible only querying the database and not using any other language. EDIT: Also I don't know the ammount of data per ID could be 3 different values or 12. It differs from ID to ID – user2123665 Dec 17 '14 at 16:35
  • Now i'm really curious as to how you are using this and why. The whole reason table column exist is to seperate different types of data from each other. if you want ints, floats, doubles, strings, blobs, all to be connected to 1 id in a column. I wonder why you would need to do that in the first place. – Roger Dec 17 '14 at 23:08
  • Lets say Value (as up there) was a date, instead of a random letter. What I'd like to do is export all that data having 1 Row per ID with all those dates as different columns of each row. – user2123665 Dec 18 '14 at 12:27