douan7529 2014-08-05 18:24
浏览 83
已采纳

INSERT并设置一个字段以复制新插入的ID

Is it possible to INSERT a row, and simultaneously set one of the fields to contain the inserted row's ID? (the "self" id)

I'm trying to avoid using multiple queries if possible (it would be 3 queries in total otherwhise)

Something like this (but probably not):

INSERT INTO thetable (email, phone, activationkey) VALUES ($email, $phone, CONCAT(THIS_NEW_ID, md5($activation) )
  • 写回答

1条回答 默认 最新

  • doulu3808 2014-08-05 18:31
    关注

    Why would you want to store duplicated data?

    You could change your table structure and just run:

    INSERT INTO thetable (email, phone, activation_suffix) 
         VALUES ($email, $phone, md5($activation))
    

    And then you have all the data you need.

    You can always concat when you query the table:

    SELECT CONCAT(id, activation_suffix) activationkey
      FROM thetable
     WHERE ...
    

    UPDATE

    On second thoughts, do you really need the id as part of the activationkey?

    I wouldn't want to give any user the id for their record in my table unless it is hashed/encrypted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器