On Thu, 3 Apr 2008, Hiep Nguyen wrote:

hi all, i have a question on insert ... select statement.

tbl_1(fld1,fld2,fld3, ....)

fld1 int primary key auto_increment not null

tbl_2(fld_a,fld_b,fld_c,...)

how do i construct my select statement so that fld1 is auto increment?

insert into tbl_1(fld1,fld2,fld3) select xxx, fld_b, NOW() from tbl_2 where fld_a = '5';

what should 'xxx' be???

my goal is to get

fld1 = auto increment
fld2 = fld_b
fld3 = NOW()


i saw someone used '1', other used null for xxx.  i'm confused.


thank you for all the helps that i got. i got this works. now i have more tables to do and wonder if this is possible to do in one statement.

let say i have 3rd table (tbl_3) with fld_i, fld_ii, fld_iii, fld_iv and this is what i'm doing now after successfully inserted into tbl_1:

select fld_i from tbl_3 where fld_i = fld_b limit 1;
if record set != 1
  insert into tbl_3 (fld_i) values (fld_b);

in other word, insert a record into tbl_3 if and only if fld_d doesn't existing in tbl_3.

here is the relationship between 3 tables:

fld_2, fld_b, and fld_i are primary key of its respective table

fld_2 = fld_b = fld_i


this is the reason that i haven't try this with JOIN clause.

thank you,
t. hiep

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

Reply via email to