【问题标题】:MySQL insert with autogenerated column in subqueryMySQL 在子查询中插入自动生成的列
【发布时间】:2012-12-10 18:51:07
【问题描述】:

我正在使用完全不同的架构将数据从一个表导入另一个表。我可以将数据从一列导入到另一列,但是插入新行时会设置一个自动生成的列。该代码只是在表上执行 count()+100001,我试图在查询中复制它,但我遇到了问题。

这是我的查询:

insert into prospect(pid,pfirst,plast,paddress,pcity,pstate,pzip,eid,pdate,pnum,pprim)
  select custid,cfirst,clast,street,city,state,zip,csrid,lastvisit,
     (select count(pid)+100000 from prospect),celphone from customer limit 140000,1000;

pnum 列(生成的列)应该是 100000、100001、100002 等,但它们最终都是 100000。我认为这是因为我只进行了一次插入,所以 count() 为 0一路走来。

我将如何完成我想要的?

【问题讨论】:

    标签: mysql sql select subquery


    【解决方案1】:

    试试这个:

    select count(pid) into @auto from prospect;
    insert into prospect(pid,pfirst,plast,paddress,pcity,pstate,pzip,eid,pdate,pnum,pprim) 
    select custid,cfirst,clast,street,city,state,zip,csrid,lastvisit,
         @auto:=@auto+1,celphone from customer limit 140000,1000;
    

    在第一个选择语句中根据您的需要更改 @auto 变量的值。

    【讨论】:

    • 必须将其设置为从 100000 开始,但效果很好。谢谢!
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2015-09-04
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多