thinkphp使用sql语句大批量新增数据(效率会比使用saveAll效率高)

  /**
   * 发送系统消息
   * @param $title 消息标题
   * @param $content 消息内容
   */
  public function systemMessage($title, $content = ‘‘) {
    $uid_arr = User::build() -> column(‘uid‘); // 用户uid列表
    $insert_list = [];
    $time = time();
    foreach ($uid_arr as $uid) {
      $message[‘uid‘] = $uid;
      $message[‘title‘] = $title;
      $message[‘content‘] = $content;
      $message[‘type‘] = ‘system‘;
      $message[‘create_time‘] = $time;
      $message[‘update_time‘] = $time;
      $insert_list[] = $message;
    }
    $sql = "insert into `cy_user_message` (`uid`,`title`,`content`,`type`,`create_time`,`update_time`) values ";
    foreach ($insert_list as $insert_data) {
      $n = 0;
      foreach ($insert_data as $key => $value) {
        $n ++;
        if ($n == 1) {
          $sql .= "(‘";
        }
        $sql .= $value;
        if ($n != count($insert_data)) {
          $sql .= "‘,‘";
        } else {
          $sql .= "‘),";
        }
      }
    }
    $sql = substr($sql, 0, strlen($sql) - 1);
    try {
      Db::query($sql);
    } catch (\Exception $e) {
      throw new \think\Exception($e -> getMessage());
    }
  }

 

thinkphp使用sql语句大批量新增数据(效率会比使用saveAll效率高)

上一篇:Photoshop为铁轨上的美女增加甜美的晨曦暖色


下一篇:Linux安装Mysql