gpt4 book ai didi

php - PHP和MySQL:如何结合UPDATE和SELECT(带有min函数)查询

转载 作者:行者123 更新时间:2023-11-29 02:30:58 24 4
gpt4 key购买 nike

我试图从MySQL表中选择一个值最小的行,并使用PHP更新该行的flag列。但这对我不起作用。如果我把它分为两个查询,它就可以工作,但是把这两个查询组合在一起是行不通的。我做错什么了?
以下是有效的方法:

$flag = 1;
$query = "SELECT MIN(value) FROM ". $myTable;
$data = mysqli_query($dbh, $query);
$dataSet = mysqli_fetch_row($data);
$value = $dataSet[0];
$queryUpdate = "UPDATE ". $myTable . " SET flag='" . $flag . "' WHERE value='" . $value. "'";
$dataUpdate = mysqli_query($dbh, $queryUpdate);

以下是我做过但失败的尝试:
//attempt #1
$flag = 1;
$query = "UPDATE ". $myTable . " SET flag='" . $flag . "' WHERE value=( SELECT MIN(value) FROM '". $myTable . "')";
$data = mysqli_query($dbh, $query);

// attempt #2
$flag = 1;
$query = "UPDATE ". $myTable . " SET flag='" . $flag . "' ORDER BY value ASC LIMIT 1";
$data = mysqli_query($dbh, $query);

// attempt #3
$flag = 1;
$query = "UPDATE ". $myTable . " T1
INNER JOIN ( SELECT MIN(value) FROM ". $myTable . " ) T2
ON T1.id = T2.id
SET T1.flag=" . $flag;
$data = mysqli_query($dbh, $query);

mysqli_error()返回
警告:mysqli_error()只需要1个参数,0给定[在$data=mysqli_query($dbh,$query)行中]
任何帮助都将不胜感激。

最佳答案

看来你已经有第三个选择了。
试试这个:

$query = "UPDATE ".$myTable." 
INNER JOIN (select min(value) as value from ".$myTable.") mintable on ".$myTable.".value = mintable.value
SET flag = ".$flag;

关于php - PHP和MySQL:如何结合UPDATE和SELECT(带有min函数)查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13259250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com