views:

28

answers:

3

An import was performed on my database which wasn't done so well. It's resulted in some fields being empty where they shoudln't, etc...

What I need to do now is move all the data from one column in one table into a column in a different table. Both tables have an ID which is a link to each rows.

For example:

Table1

id | linkID | email
---+--------+-------------------
1  |  7     | 
---+--------+-------------------
2  |  3     | [email protected]

Table2

id | email
---+-----------------
7  | [email protected]
---+-----------------
3  | [email protected]

I was going to write a small PHP script to pull out all the rows from Table2 and UPDATE Table1 using the id and email. But I was wondering if this could be done entirely through SQL queries?

A: 
update table2 set email = (select email from table1 where table1.linkId = table2.id)
Anatoly G
are you sure that TOP works in mysql ? and why you what update table2 ?
Cesar
not sure if that one works, but I am sure there is a TOP.
Anatoly G
A: 
update table1 set email = (select email from table2 where table1.linkID=table2.id);
Cesar
A: 
update table1
inner join table2 on table1.linkID = table2.id
set table1.email=table2.email;

if you only want to set the empty emails:

update table1
inner join table2 on table1.linkID = table2.id
set table1.email=table2.email
where table1.email = '';
ceteras