tags:

views:

88

answers:

3

hi, i would like to know how this can be done. lets say i have this in my database "first" and if i want to update and add the "second" in which i can have this format "first, second". i used this UPDATE table SET number="second" but it removes the "first". i am using php and mysql, thank you

+5  A: 

I guess you mean:

// if number is "first", it will become "first, second"
UPDATE table SET number = CONCAT(number, ", second")

but I don't think that's what you need. Can you explain what you are trying to do?

Tatu Ulmanen
+1, I think this is what he needs.
Alix Axel
thats what i needed, thanks. :)
Sam
FWIW: if you're trying to store a list in a database, don't do it this way. Instead store the data as separate rows and use GROUP_CONCAT to extract the result when you do a select.
Jason S
Why using a database if storing several Items in one column of all rows? I don't see any sense in the statements of both answers. As Tatu allready mentioned: please explain your question in more details.
Ice
all im trying to do is i want to store some names (maximum of 5) in a same category and then use explode(', ', $names); and show them using "for" loop. u guys think this is not a good method?
Sam
Well, according to how databased *should* be designed, the names should be in their own table with a reference to entry in a another table they relate to.
Tatu Ulmanen
+1  A: 
UPDATE table SET number=concat_ws(",", number, "second")
Galen
This is actually "first,second" and not "first, second".
Alix Axel
you can easily just add a space after the , in the function call
Galen
A: 

The update answer you've chosen gives you what you want. I want to chime in with somethings that you may find useful.

Since you are obviously learning, let's ensure that you get some related items covered as well:

  1. Database and Code Backup: Keep the original safe before you start having your fun with it. If something goes wrong, you can always restore from backup and move on.

  2. Souce Code Control: Keep your code under Source Code Control so that you can go back to previous versions as and when required.

  3. Separation of Prod and Dev: If you play in Production, you have the ability to cause a lot of damage. Make double triple sure that you are working in an isolated environment.

Raj More