dongxiqian2787 2014-01-03 19:09
浏览 149
已采纳

如何在表中创建插入,其中部分数据来自一个表并且其余部分已填充

I have table messages where I am trying to create new messages to users. Algorythm looks like:

1) Get all user e-mails (they act as user id and is primary key in table user)

2) For each e-mail insert message (same to all), flag is read (false) and time (NOW())

How to do this? I wrote next, but I am sure that such line is wrong.

INSERT INTO `messages` (`email`, `message`,`isread`,`createdat`) SELECT DISTINCT `email` FROM `users`, ?, false, NOW()

P.S. I am doing this via PHP, so ? will be message text. For messages primary key is id that is AUTO_INCREMENT. Database: MySQL

  • 写回答

2条回答 默认 最新

  • douqianni4080 2014-01-03 19:15
    关注

    Add your literal value to the select clause as shown below.

    Take a look at this sqlFiddle

    INSERT INTO `messages` (`email`, `message`,`isread`,`createdat`) 
    SELECT DISTINCT `email`, '?', false, NOW() 
    FROM `users`
    

    And the samething, but in PHP

    <?php
    
      $myMessage ="...";
    
      $sql = sprintf("
                      INSERT INTO `messages` (`email`, `message`,`isread`,`createdat`) 
                      SELECT DISTINCT `email`, '%s', false, NOW() 
                      FROM `users`", 
                      $myMessage);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置