dongzhi4690 2017-11-11 12:34
浏览 169

两个INSERT INTO语句用于同一行SQL / php

I have been trying so hard and I can't find a solution for this problem. I have two different INSERT statements:

INSERT INTO `logList`(`User_ID`) SELECT `User_ID` FROM `userList` WHERE keyname='somevalue';
INSERT INTO `logList`(`ttime`) VALUES (CURRENT_TIMESTAMP);

If I execute them, they will of course insert 2 different rows. How can I make a single INSERT that returns only one row? Thank you in advance.

  • 写回答

3条回答 默认 最新

  • douxiangshi6568 2017-11-11 12:36
    关注

    I am going to assume that this is what you really want:

    INSERT INTO logList (User_ID, ttime)
        SELECT User_ID, CURRENT_TIMESTAMP
        FROM userList
        WHERE keyname = 'somevalue';
    

    I doubt you want two separate rows, one with the user and NULL for time and the other with NULL for user and a value of the time.

    I should note that you can define the loglist so ttime is defaulted to the current timestamp on the insert. The use of such defaults is described in the documentation.

    If you did that, then:

    INSERT INTO logList (User_ID)
        SELECT User_ID
        FROM userList
        WHERE keyname = 'somevalue';
    

    would always set ttime.

    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题