BR
asked on
mysql sql statement - SQL INSERT INTO SELECT
I have a table named table1 which has the names of customers inside.
In table1 and table2 I have unique customer id which I can use when I copy customer names,
I want to copy all the names from table1 into table2 with the related customer id?
how should I write the sql statement?
In table1 and table2 I have unique customer id which I can use when I copy customer names,
I want to copy all the names from table1 into table2 with the related customer id?
how should I write the sql statement?
use Update if the customer Id already exists in table2
use Insert into if the customer id not exists in table2
use Insert into if the customer id not exists in table2
something like this should do if and only if your customer_id field is unique in the destination table2
---
if you don't have the index, try something like this
insert into table2 select customer_id, customer_name from table1 on duplicate key update customer_name = VALUES (customer_name)
---
if you don't have the index, try something like this
lock database
create table temp as select table2.*,table1.customer_name from table2 inner join table1 on table1.customer_id = table2.customer_id
rename table2 to t , temp to table2
drop table t
for update:
for insert:
code not tested, pls customize accordingly.
Update table2 a
inner join table1 b
on a. customerid = b.customerid
set a.customername = b.customername
for insert:
Insert into table2 (customerid, customername)
select a.customerid, a.customername
from table1 a left join table2 b
on a.customerid = b.customerid
where b.customerid is null
code not tested, pls customize accordingly.
ASKER
Dear Ryan Chong,
what does a mean? I mean what is table2 a?
a = column?
what does a mean? I mean what is table2 a?
a = column?
nope, a is the alias of the table.
we can write:
we can write:
table2 a
as table2 as a
did you try using the "on duplicate key update" way ? unless there is something not workable, it is the only way to do it in a single query
ASKER
Dear skullnobrains, Ryan Chong
I'm sorry, I couldn't manage it..
My actual table name is "bayibilgileriiki" which has the data of the customer names.
and the a column name is "adi"
I want to copy "adi" coloumn to "KullaniciBilgileriiki" table's "isim" column.
"isim" column is now NULL.
I have unique customer id on both tables which coloumn name is "uid" for both of them
could you help me please?
I'm sorry, I couldn't manage it..
My actual table name is "bayibilgileriiki" which has the data of the customer names.
and the a column name is "adi"
I want to copy "adi" coloumn to "KullaniciBilgileriiki" table's "isim" column.
"isim" column is now NULL.
I have unique customer id on both tables which coloumn name is "uid" for both of them
could you help me please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
just for reference since you already got a working solution
insert into KullaniciBilgileriiki ( uid , isim ) select uid,adi from bayibilgileriiki on duplicate key update isim = VALUES (isim)
insert into KullaniciBilgileriiki ( uid , isim ) select uid,adi from bayibilgileriiki on duplicate key update isim = VALUES (isim)
ASKER