利用另一张表update更新字段_insert语句使用另一张表数据

前言

1、insert语句

1.1、Insert into

  语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:
举例:Insert into Table2(a, c, d) select a,c,5 from Table1




INSERT INTO coupon_item_good_admin_user
( couponItemGoodId, couponAdzoneId, pubAdminId, pubAdminName, volume,
  cdate, udate, status)
  SELECT  c.id,c.couponAdzoneId,c.createAdminId,c.createAdminName,0, now(),now(),1
  from coupon_item_good c;



1.2、select into

SELECT vale1, value2 into Table2 from Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
举例select a,c INTO Table2 from Table1

select ename,sal,job into pname,psal,pjob from emp where empno = eno;

2、update

UPDATE  coupon_item_good b set b.volume =
    (
    SELECT m.orderSize from
      ( SELECT  COUNT(c.itemId) as orderSize, c.itemId  as itemId from coupon_taoke_data c  WHERE  c.orderStatus not in ('订单失效') and c.status = 1 GROUP  by c.itemId  )  m WHERE   m.itemId = b.itemId )  
WHERE  b.itemId IN (40762167681);  





如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
posted @ 2018-07-26 15:36  HealerJean  阅读(398)  评论(0编辑  收藏  举报