如果你想插入10個記錄在相同的條件:
INSERT INTO `clubs`(`id_leagues`, `name`, `created`)
SELECT id_leagues, name, created FROM clubs WHERE id = 36 LIMIT 10
並與另一條件:
INSERT INTO `clubs`(`id_leagues`, `name`, `created`)
SELECT id_leagues, name, created FROM clubs WHERE id IN (36,37,38,40,45,55)
而在PHP中,如果你的查詢過於複雜,使用INSERT INTO ... SELECT
,您可以選擇所需的數據,形成一個批量插入查詢並執行它:
$query = "SELECT id_leagues, name, created FROM clubs WHERE id = 36 LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM));
$data = array_fill(0, 9, "('".mysql_real_escape_string($row[0])."', '".mysql_real_escape_string($row[1])."', '".mysql_real_escape_string($row[2])."')");
$query = "INSERT INTO `clubs`(`id_leagues`, `name`, `created`) VALUES " . implode(',', $data);
mysql_query($query);
不,不適用於限制10.我不想插入不同的列,但同樣10次。 – helle 2012-03-23 09:30:33
如果可以使用PHP,我會選擇使用它的一行,準備批量插入並執行它。 – Minras 2012-03-23 10:02:14
是的,我知道如何做到這一點。問題是,你如何處理與MySQL的問題... – helle 2012-03-23 10:08:39