If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > insert from a query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-03-04, 16:32
cb1 cb1 is offline
Registered User
 
Join Date: May 2004
Posts: 4
insert from a query?

can i do this in mysql?

tables t1 and t2 are exactly the same except that t2 will have rows in it that aren't in t1. also, i'm doing this through the mysql odbc 3.51 driver.

pk_id is the primary key (an identity column from sql server).

insert into t1 select * from t2 where pk_id not in (select pk_id from t1)
Reply With Quote
  #2 (permalink)  
Old 05-03-04, 16:39
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
You can't use that syntax, but you can get the same effect using:
Code:
INSERT INTO t2
   SELECT t1.*
      FROM t1
      LEFT OUTER JOIN t2
         ON t2.pk_id = t1.pk_id
      WHERE  t2.pk_id IS NULL
It is just a different way of thinking about the same thing.

-PatP
Reply With Quote
  #3 (permalink)  
Old 05-03-04, 21:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
a different way? i'll say

cb1 wants to insert into t1 any t2 rows that t1 doesn't already have

what you proposed inserts into t2 any t1 rows that t2 doesn't already have

"just a different way of thinking", eh

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-03-04, 23:13
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Oooops! My bad.

-PatP
Reply With Quote
  #5 (permalink)  
Old 05-04-04, 00:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
no prob

you are so infrequently wrong, i just wanted to make the correction and keep your record clean

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 05-04-04, 12:32
cb1 cb1 is offline
Registered User
 
Join Date: May 2004
Posts: 4
thanks for the help. i got it working properly.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On