从另一个表中插入数据

时间:2013-11-24 02:45:47

标签: sql sqlplus

我一直坚持这个问题 这里有方向

被邀请参加“蒙娜丽莎”晚会的客户现在被邀请参加2014年5月1日的蒙娜丽莎晚会。显示insert命令和生成的Invite表。

以下是两个表

SQL> select * from invite;

GALA_DATE   PAINTING_NAME          CUSTID
----------- ---------------------- ----------
10-nov-2013 Watercolors                  1430
15-nov-2013 Woman                        1502
15-nov-2013 Woman                        1619
05-dec-2013 Watercolors                  1207
22-dec-2013 Sunflowers                   1806
22-dec-2013 Sunflowers                   1904
31-dec-2013 Fiddler                      1236
31-dec-2013 Fiddler                      1280
05-jan-2014 Mona_Lisa                    1111
05-jan-2014 Mona_Lisa                    1502
25-jan-2014 Madonna                      1806
25-jan-2014 Madonna                      1822
25-jan-2014 Madonna                      1904
18-feb-2014 Maya                         1619
18-feb-2014 Maya                         1822
18-feb-2014 Maya                         1904
28-feb-2014 Mona_Lisa                    1502
30-apr-2014 Lovers                       1207
30-apr-2014 Lovers                       1280
30-apr-2014 Lovers                       1822
30-apr-2014 Lovers                       1904

SQL> select * from customer;

CUSTID CUSTNAME    CUSTBDATE   CUST_TYPE BENEFACTOR  DOCENT

  1301 Disney      01-nov-1980 NM        No      No
  1806 Garcia      31-dec-2000 VIP       Yes     No
  1502 LaGardia    15-jan-1960 VIP       Yes     Yes
  1207 Perry       20-jan-1960 VIP       Yes     Yes
  1280 Beecham     31-dec-1979 VIP       Yes     No
  1822 Becker      30-jan-1967 VIP       Yes     Yes
  1140 Klim        05-apr-1990 NM        No      No
  1509 Roberts     21-jul-1989 VIP       Yes     No
  1619 Robins      20-feb-1990 VIP       Yes     Yes
  1111 Bardot      28-feb-1970 VIP       No      No
  1515 David       18-apr-1980 NM        No      No
  1701 Martin      20-aug-1972 RM        No      No
  1904 Gross       30-sep-1975 VIP       Yes     Yes
  1236 Brooks      23-sep-1975 VIP       Yes     No
  1430 Todd        15-jul-1982 VIP       Yes     Yes

我已经尝试过了

 insert into invite(gala_date,painting_name,custid)
 select invite.gala_date,invite.painting_name,invite.custid
 from invite,customer
 where invite.gala_date='05 Jan 2014' and invite.painting_name='Mona_Lisa'
 and customer.custid=invite.custid
 and customer.custid not in
 (select custid from invite where gala_date in('05 Jan 2014')
 and painting_name in('Mona_Lisa'));

创建了0行。

但是你可以看到结果产生“0行创建” 有什么想法吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个

insert into invite(gala_date,painting_name,custid)
 select '01/05/2014', 'Mona_Lisa', a.custid
 from customer a
left join invite b on (a.custid = b.custid and b.PaintingName = 'Mona_Lisa'
where b.custid is null

我认为这会让你接近,可能需要调整它,因为我没有检查

答案 1 :(得分:0)

你只需要使用not exists来获得你想要的结果。

insert into invite(gala_date,painting_name,custid)
 select '01/05/2014', 'Mona_Lisa', customer.custid
From customer
Where Not Exists (select '' from invite where invite.custid = customer.custid and painting_name = 'Mona_Lisa')