我想從一個SQL數據庫表中插入數據到另一個。它需要以指定的順序插入,因爲我打算將表中的數據提取到csv文件以創建目錄。我在主表中有超過50000個項目,並且想要將約500個項目提取到新表格中。從SQL表中插入指定順序
我已經創建了一個表單來輸入參考編號(ref),它創建了一個參考編號數組。
<form action="list.php" method="post">
<p>List Number <input name="list_no" type="text"></p>
<table align="center" width="50%">
<tr>
<td><input name="item[]" type="text" style="width: 80px"></td>
<td></td>
<td><input name="item[]" type="text" style="width: 80px"></td>
<td></td>
<td><input name="item[]" type="text" style="width: 80px"></td>
<td></td>
<td><input name="item[]" type="text" style="width: 80px"></td>
<td></td>
<td><input name="item[]" type="text" style="width: 80px"></td>
</tr>
</table>
<p>Step 4: Create List.<input name="Submit1" type="submit" value="Create List" /></p>
在list.php的我已經創建的代碼在我的名單...取決於數量的數據庫中創建一個新表
表單上輸入。我從表中分配給陣列
$item=$_POST['item'];
我知道在$項目是在我進入它的形式排列的數據,因爲我已經使用$ item_list =破滅數據(「」,$項) ;並echo $ item_list來檢查輸入數據的順序,並用逗號分隔數據。
我list.php的頁面,然後使用代碼
$insert="INSERT INTO list$list_no (ref, description, price_s)
SELECT ref, description, price_s
FROM stock
WHERE ref IN ($item_list)";
mysql_query($insert) or die(mysql_error());
進入項目到新表。它輸入所有已請求的項目,但似乎按照ref的順序輸入它們,而不是按照我將它們輸入原始表單的順序輸入。
關於如何糾正這個問題的任何想法?
很多預先感謝!
聽起來像ref是一個索引。你能不能在你的SELECT查詢中添加一個ORDER BY子句來寫入csv? – CBusBus 2012-02-15 09:44:59
你的主要關鍵是什麼?你可以發佈你的SQL模式嗎? – CompanyDroneFromSector7G 2012-02-15 09:45:06
以特定順序插入沒有任何意義。表格中的行不是**排序。 – 2012-02-15 09:58:44