MySQL Forums
Forum List  »  PHP

'Merging' Columns?
Posted by: Alex Davison
Date: August 15, 2005 06:36AM

I am trying to get several columns within a database to 'merge' together.

For example, I have two columns named "mon_1" and "mon_1_room", with data "10F/Hi1" and "MC C113" in them respectively, what I want is for all the data to be in the "mon_1" column, preferably with a # in between the two bits of data, (i.e 10F/Hi1#MC C113)

I've managed to write a piece of code but it takes about 30 minutes to execute (There are 1700 records and 25 sets of columns) and it doesn't seem to be working quite right. I also divided it into days so it was slightly more manageable. I've put an example of what I concocted at the bottom, but I do warn you, it's nasty.

If someone could point me in the right direction or give me some tips I'd greatly appreciate it, thanks for your help in advance,

Alex Davison
ICT Technician
Copleston High School


//NASTY CODE!!!

$day = "mon";
$period = 0;
do {
$period++;
$oldlesson = $day."_".$period;
$oldlessonroom = $day."_room_".$period;
$sql = "SELECT ".$oldlesson.", ".$oldlessonroom." FROM sims_import";
$select_result = mysql_query($sql);

$count = mysql_num_rows($select_result);
if ($count > 0) {
while ($row = mysql_fetch_array($select_result)) {
//echo $row[0]." ".$row[1]."<BR>";
$lesson = $row[0];
$lessonroom = $row[1];

$lesson_code = $lesson."#".$lessonroom;
$update = "UPDATE sims_import SET ".$oldlesson." = '$lesson_code' WHERE ".$oldlesson." LIKE '$lesson'";
echo $update."<BR>";
$sqlupdate = mysql_query($update);


}
}



} while ($period < 5);

Options: ReplyQuote


Subject
Written By
Posted
'Merging' Columns?
August 15, 2005 06:36AM
August 16, 2005 08:24AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.