Solved

mysql sql statement - SQL INSERT INTO SELECT

Posted on 2016-10-20
11
44 Views
Last Modified: 2016-10-20
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?
0
Comment
Question by:Braveheartli
  • 4
  • 4
  • 3
11 Comments
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
I have a unique id on the both table : customerid
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
use Update if the customer Id already exists in table2

use Insert into if the customer id not exists in table2
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
something like this should do if and only if your customer_id field is unique in the destination table2

insert into table2 select customer_id, customer_name from table1 on duplicate key update customer_name = VALUES (customer_name)

Open in new window


---

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

Open in new window

0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
for update:

Update table2 a
inner join table1 b
on a. customerid = b.customerid
set a.customername = b.customername

Open in new window


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

Open in new window


code not tested, pls customize accordingly.
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
Dear Ryan Chong,
what does a mean? I mean what is table2 a?
a = column?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
nope, a is the alias of the table.

we can write:
 table2 a

Open in new window

as
 table2 as a

Open in new window

0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
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?
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
you should able to do an update like this?

Update KullaniciBilgileriiki a
inner join bayibilgileriiki b
on a.uid= b.uid
set a.isim = b.adi

Open in new window

let me know if you got any syntax error, etc
0
 
LVL 1

Author Closing Comment

by:Braveheartli
Comment Utility
thank you
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
The viewer will learn how to dynamically set the form action using jQuery.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now