Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按指定顺序从SQL表插入_Php_Mysql_Sql - Fatal编程技术网

Php 按指定顺序从SQL表插入

Php 按指定顺序从SQL表插入,php,mysql,sql,Php,Mysql,Sql,我正在尝试将数据从一个SQL数据库表插入到另一个SQL数据库表中。它需要以指定的顺序插入,因为我计划将表中的数据提取到csv文件以创建目录。我的主表中有50000多个项目,希望将大约500个项目提取到新表中 我已经创建了一个表单来输入参考号(ref),它创建了一个参考号数组 <form action="list.php" method="post"> <p>List Number <input name="list_no" type="tex

我正在尝试将数据从一个SQL数据库表插入到另一个SQL数据库表中。它需要以指定的顺序插入,因为我计划将表中的数据提取到csv文件以创建目录。我的主表中有50000多个项目,希望将大约500个项目提取到新表中

我已经创建了一个表单来输入参考号(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>
我知道$item中的数据是按照我在表单中输入的顺序输入的,因为我使用了$item_list=infrade(“,”,$item);和echo$item_list检查输入数据的顺序,并获取用逗号分隔的数据

然后,My 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\u query($insert)或die(mysql\u error())

将项目输入新表。它输入所有已请求的项目,但似乎是按照ref的顺序输入的,而不是按照我在原始表单中输入它们的方式的顺序

有没有关于如何纠正这个问题的想法


非常感谢

您需要通过选择查询中的
ref
字段
ORDER BY
FIND\u in\u SET
将保留订单:

$insert = sprintf("INSERT INTO list%s (ref, description, price_s) 
                   SELECT ref, description, price_s 
                   FROM stock 
                   WHERE ref IN ('%s')
                   ORDER BY FIND_IN_SET(ref, '%s')", $list_no, $item_list, $item_list);

编辑:你真的应该改变你的数据库设计。这不是一个好方法。

以特定顺序插入没有任何意义

表中的行未排序

您必须在检索期间(从“列表”表)使用
ORDER BY对它们进行排序

MySQL在插入过程中将重新排序行,因为它在主键上有聚集索引

它需要按指定的顺序插入

事实并非如此

因为我计划将表中的数据提取到csv文件以创建目录

欢迎您使用ORDER BY进行此操作

我的主表中有50000多个项目,希望将大约500个项目提取到新表中

你不会再想要它了。有两张日期相同的桌子是没有意义的。

为什么不能从原始表中生成CSV?

听起来ref是一个索引。您能否将ORDER BY子句添加到SELECT查询以写入csv?您的主键是什么?您能发布您的SQL模式吗?以特定顺序插入没有任何意义。表中的行未排序。@a_horse_,没有名称:这应该是答案……谢谢您的快速回复。%s代表什么?我刚刚运行了您建议的查询,它将表单中的第一项添加到新表中,但没有添加任何其他内容。你认为数据库应该如何设计?Thansk%s-表示一个字符串。因此,第一个%s将替换为$list\否。请查看,谢谢,这很有意义。在您发布的文章中说sprintf与printf的工作原理相同,所以我用printf替换了sprintf,并收到了这个错误:“插入列表102(参考,描述,价格)从库存中选择参考,描述,价格,其中参考('45154646082680668066111865386')按查找集(参考,'451546460826806636611865386')排序您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解第1行“197”附近使用的正确语法”。我m仍然只是将第一项插入表中。
$insert = sprintf("INSERT INTO list%s (ref, description, price_s) 
                   SELECT ref, description, price_s 
                   FROM stock 
                   WHERE ref IN ('%s')
                   ORDER BY FIND_IN_SET(ref, '%s')", $list_no, $item_list, $item_list);