Page 1 of 1
Updating table1.field from table2.field
Posted: Tue Jan 31, 2006 8:57 am
by eyespark
Hi
I have another newbie question for the comunnity. I have 2 tables (let us say table1 and table2) in my database, both have email field. I would like to import emails from table1.email into table2.email but only those that are not already in table2.email. How would php function look like?
Thanks in advance
Posted: Tue Jan 31, 2006 10:33 am
by raghavan20
your question is not clear...please use examples..
Posted: Tue Jan 31, 2006 11:20 am
by Chris Corbyn
To do in one query this is advanced stuff
You can use an insert..select along with a subquery if you're using MySQL4.1 or higher.
Untested:
Code: Select all
insert into
table2 (email)
select
email
from
table1
where
email not in (select email from table2)
Posted: Tue Jan 31, 2006 12:01 pm
by josh
UNION, the union syntax says it selects as if a distinct of both tables together, so union your tables on the email:
(select `email` from a) UNION (select `email` from b)
Posted: Tue Jan 31, 2006 1:29 pm
by timvw
Place a unique constraint on the target column and blindfully insert-select from the source column.
Posted: Tue Jan 31, 2006 3:05 pm
by raghavan20
jshpro2 wrote:UNION, the union syntax says it selects as if a distinct of both tables together, so union your tables on the email:
(select `email` from a) UNION (select `email` from b)
this will not work because it will choose distinct entries from table1 and table2 which is not really what he is asking...d11wtq's way is the solution for this.....or you could do timvw's as well....that was a nice idea....