如何同时在表中插入多条记录?

use*_*600 5 php sql yii

我有两个表AccommodationFacility,其连接在与第三表中的多对多的关系,Accommodation_facility.

  • 住宿(住宿_id,住宿类型,名称)
  • 设施(facility_id,facility_name)
  • Accommodation_facility(accommodation_id,facility_id)

使用Yii,如何将多个数据记录插入Accomodation_facility表中?

Bil*_*lly 11

使用循环插入非常慢.假设您要插入5000行,这将花费大约6分钟(每个记录单独插入).最好使用单个查询插入数据:

$values = '(null, "your string"), (null, "next string"), (null, "third string")';
$sql = 'INSERT INTO table_data (id, data) VALUES ' . $values;
$command = Yii::app()->db->createCommand($sql);
$command->execute();
Run Code Online (Sandbox Code Playgroud)

那将需要1/10的时间.


Eir*_*oem -2

由于您的问题被标记为“yii”,我猜您正在使用 Yii 框架。查看文档中的 Active Records - http://www.yiiframework.com/doc/guide/1.1/en/database.ar

按照文档为您的表设置 AR 类,并在提交复选框列表时简单地循环遍历您发布的数据。在此循环中,您可以为要为其插入数据的表创建、填充并保存 AR 对象。