Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 MySQL,其中插入时按顺序排序_Php_Mysql_Forms_Insert - Fatal编程技术网

Php MySQL,其中插入时按顺序排序

Php MySQL,其中插入时按顺序排序,php,mysql,forms,insert,Php,Mysql,Forms,Insert,我查看了文档,似乎没有找到任何描述如何做我正在尝试的事情的内容。再说一次,我没有发现任何东西说你也不能 $querytotal = "insert into offer_det where where fname = '".$fname."' and lname = '".$lname."' ORDER BY id DESC LIMIT 1 (`t1`, `t2`, `t3`, `t4`) values($t1, $t2, $t3, $t4)"; $resultotal = mysql_quer

我查看了文档,似乎没有找到任何描述如何做我正在尝试的事情的内容。再说一次,我没有发现任何东西说你也不能

$querytotal = "insert into offer_det where where fname = '".$fname."' and lname = '".$lname."' ORDER BY id DESC LIMIT 1 (`t1`, `t2`, `t3`, `t4`)
values($t1, $t2, $t3, $t4)";
$resultotal = mysql_query($querytotal);
我的问题是:这是一个合适的插入语句吗?本质上,我只需要匹配名字和姓氏,然后为它们选择最近的表条目,因为可能有多个表条目具有相同的名字和姓氏。从这里开始,我需要做的就是用变量$t1-4插入四个值t1-4

我看到了,但没有什么意义


感谢您在这一点上与我交流。

您的sql不正确。试试这个

重新编辑:

$querytotal = "insert into offer_det (`t1`, `t2`, `t3`, `t4`) values(’$t1’, ’$t2’, ’$t3’, ’$t4’) where  id = (SELECT max(id) FROM offer_det where fname = '".$fname."' and lname = '".$lname."')"; 
$resultotal = mysql_query($querytotal); 

但是你确定你需要的是插入而不是更新查询吗?

我想你需要这样的东西

$querytotal = "insert into offer_det (t1, t2, t3, t4) " .
"select t1, t2, t3, t4 from offer_det where fname = '$fname' and lname = '$lname' order by id desc limit 1";
$resultotal = mysql_query($querytotal);

我不知道你想要什么,但也许你需要这样的东西

    $query = "Insert into offer_det (t1, t2, t3, t4)
               (SELECT $t1, $t2, $t3, $t4 FROM offer_det where
              fname = '".$fname."' and lname = '".$lname."' ORDER BY ID DESC Limit 1)";

这将根据来自同一个表的fname和lnam的最后一个结果在此表中插入(t1、t2、t3、t4)。

您正在尝试基于从另一个表中选择进行插入吗?这是可能的。我想你有一个太多的地方…哈哈,上帝,我怎么会错过复制品呢where@user1718270是在将sql放入脚本之前对其进行测试的绝佳资源。这将在fname和lname的所有条目中插入值,但我只需要将它插入到该组合的最新条目中。这就是我凭身份证订购的原因。对不起。我没有意识到这一点。我已经修改了查询。那应该行得通,我不确定。但是当您使用max时,它不会查询整个表,而是只查询一条记录。而且我也不是在查询所有的列。一行一列。所以它不会有任何区别。除非最后插入的记录是为在fname和lname上匹配的人创建的,否则max(id)子查询将无法工作。是的。同意。我已经重新编辑了查询。那么,按订单应该是更好的选择。