-1

I want to convert all columns data into rows data.

id | division name | student_name1 | student_name2 | student_name3 | student_name4 |
1  | blah blah         | abc                  | XYZ                   | jlm                      | tmn                    |

This table I have created and values are save using array.

I want to save all student name in one column like below:

id | division name | student_name |
1  | blah blah        | abc                   |
2  | blah blah        | XYZ                  |
3  | blah blah        | jlm                    |
4  | blah blah        | tmn                   |

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ShriD
  • 43
  • 1
  • 12
  • I would suggest looking at using temporary tables, please give a bit more information on what you've tried so far and any issues you've come across? – llanato Mar 22 '13 at 12:30
  • Similar questions asked and answered see: http://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns – xQbert Mar 22 '13 at 12:32
  • You could look into relating the tables somehow, such as a division table & student table & a relationship table. Then query the relationship table for the student to division match – UnholyRanger Mar 22 '13 at 12:33
  • @xQbert actually it is the exact opposite – ITroubs Mar 22 '13 at 12:34
  • basically i have only one table of student information with there division using array i saved values in database but it save like first table which i shown in my question so it increase my columns i don't want that i want to save that information only in three like second table which i shown in my question. – ShriD Mar 22 '13 at 12:37
  • Please show us some relavent code you used. [What have you tried](http://www.whathaveyoutried.com) to accomplish this? – UnholyRanger Mar 22 '13 at 12:38
  • This other answer might help [Simple Pivot](http://stackoverflow.com/questions/5078916/simple-pivot-of-columns-to-rows-in-mysql) – mconlin Mar 22 '13 at 12:39

1 Answers1

0

First table into array $students, pseudocode:

for($i=2; $i<sizeof($students);$i++) {
  mysql_query("insert into 'newTable' ('division name', 'student name') values ($students[1], students[$i]) "); 
}
trafalgar
  • 716
  • 3
  • 9
  • 21