PDA

View Full Version : How would I join two fields in one db table ?


student101
07-27-2008, 07:33 PM
How would I join two fields in one db table ? - (wish there was a drag & drop function)

Two fields in same table; usr_login
usr_name => name
usr_surname => surname

Would like to join them;
name surname in one field called usr_nameIs there a join or merge query that can do this like;
merge usr_name," ",usr_surname into usr_name
Cheers

masterofollies
07-27-2008, 07:55 PM
So you want surname in front of name, but in the same field?

So say they enter Mr. in a field, then their name Jason.

So in the field you want it saying Mr. Jason?

student101
07-27-2008, 08:05 PM
So you want surname in front of name, but in the same field?
So say they enter Mr. in a field, then their name Jason.
So in the field you want it saying Mr. Jason?
No, Jason Mr.
Well not even, it's name then surname or lastname

So it's Jason Fletcher (If Jason's surname was Fletcher)

Currently the fields are seperate like this;
usr_name
usr_surname

masterofollies
07-27-2008, 09:16 PM
It should be something like this. For the professionals out there can help correct my spelling mistakes.


$name = explode(",", $row["usr_name"]);

array_push($name, $new_usr_name); //Add usr name.

$new_name = implode(",", $name); //Add comma between names

$userid = $row["id"];

((Add your update query here))

That would add your usr_name, then a comma after it.

Actually let me think of a better way, I think this is getting off topic.

student101
07-27-2008, 09:31 PM
Cool, so there isn't actually a method to update / merge the database columns other than using a PHP query?

I thought I may have needed to do;
ALTER TABLE table_name
ADD column_name datatype
Insert into column_name from other column
DROP COLUMN column_name
Or something like this?
UPDATE tbl_login SET
usr_name= SUBSTRING(usr_name, 1, usr_surname)

oesxyl
07-27-2008, 09:38 PM
No, Jason Mr.
Well not even, it's name then surname or lastname

So it's Jason Fletcher (If Jason's surname was Fletcher)

Currently the fields are seperate like this;
usr_name
usr_surname
something like that?

update yourtable set usr_login = concat(usr_name,' ',usr_surname)



regards

student101
07-27-2008, 09:42 PM
something like that?
update yourtable set usr_login = concat(usr_name,' ',usr_surname)
regards
THANK YOU!!!
This worked: update tbl_login set usr_name = concat(usr_name,' ',usr_surname)

Wish I had access to that resource you have.

Cheers

oesxyl
07-27-2008, 10:18 PM
Wish I had access to that resource you have.

Cheers
I'm not sure if this is what you wish, :)

http://dev.mysql.com/doc/refman/5.0/en/index.html

more specific for this case:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

anyway if there is something unclear feel free to ask. :)

regards

student101
07-27-2008, 10:41 PM
Thank you, that will help me next time - I hope.

Cheers