What am I missing 

INSERT INTO table1 (column names.....) 
SELECT VALUES......
FROM table2 
WHERE primary id = insert value

You will have to put in your real table name and column names.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Friday, September 23, 2005 11:59 AM
To: DJ
Cc: mysql@lists.mysql.com
Subject: Re: insert subquery

DJ <[EMAIL PROTECTED]> wrote on 09/23/2005 12:49:35 PM:

> [EMAIL PROTECTED] wrote:
> 
> >DJ wrote on 09/23/2005 12:22:58 PM:
> > 
> >
> >>i want to insert a row into table1 only if the value being inserted
on 

> >>table1 exists on table2 primary id.
> >>
> >>can i do this with subquery?
> >>
> >>thanx.
> >>
> >> 
> >>
> >
> >Depending on what version MySQL you are using, probably not. A very 
robust 
> >method of doing what you propose is to allow MySQL to do it for you
by 
> >establishing a Foreign Key from table1 to table2. One drawback is
that 
> >both tables need to be InnoDB (which you may not want to support). 
> >
> >What version are you using and what is the possibility of using
InnoDB 
> >with your appliation?
> >
> >Shawn Green
> >Database Administrator
> >Unimin Corporation - Spruce Pine
> >
> >PS: always CC the list on all responses (unless you intentionally
mean 
to 
> >take the conversation off-list)
> > 
> >
> i am using 4.1.x
> hmm.. maybe it's easier if i just check the id is in table2 before 
> inserting into table1.
> not really a big deal just looking to create shortcuts without running

> multiple queries..
> 
> 

With a foreign key defined, you only need to run one query. That's why I

mentioned it. ;-)  Your way works, too.

Shawn Green
Database Administrator
Unimin Corporation - Spruce Pine


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to