SQL Query question... Hi, hope someone can help with this I have a table called
ID: 3731807 • Letter: S
Question
SQL Query question... Hi, hope someone can help with this I have a table called customers with columns: ID, firstname, lastname, phone, email. The table has 9 rows. I have made a duplicate of this table called customer_copy and i have added a new column to the customer_copy table called Fullname. I am required to concat the firstname and lastname columns of the customers table and add those values to the customers_copy table in the fullname column.
I wrote this sql query: INSERT INTO `customers_copy`(`fullname`) SELECT CONCAT(`firstname`, ' ', `lastname`) FROM `customers`
The problem is that this query creates 9 new rows and adds the data to these extra rows in the fullname column rather than adding to the fullname column of the original rows. Is there a way to add these values starting at the first, original row? without adding additional rows?
Explanation / Answer
Please use this below query to store the full name.
Since, you already have data from 'customers' table into 'customers_copy' table except the 'fullname', you would have to update the rows in 'customers_copy' table instead of new inserts.
This can be acheived by a UPDATE query and joining 'customers' and 'customers_copy' tables over ID ( Unique).
SQL QUERY : -
UPDATE customers_copy a
JOIN customers b ON a.ID = b.ID
SET a.fullname = CONCAT(b.firstname,' ',b.lastname)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.