我需要以下内容来更新具有最小值的行,并且必须将其限制为1现在它将更新所有具有最小值的列,如果它与我尝试放入LIMIT 1相同但是我一直在收到错误。 (解决方法“INNER JOIN”的原因因为该表已经在使用,所以我不能在没有它的情况下更新它。
请参阅指出错误的评论://THIS ONE DON'T WORK
。
<?php
require('includes/config.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//if no page clicked on load home page default to it of 1
if(!isset($_GET['p'])){
$q = mysql_query("SELECT * FROM infor WHERE isCount=(select min(isCount) from infor) ");
} else { //load requested page based on the id
$id = $_GET['p']; //get the requested id
$id = mysql_real_escape_string($id); //make it safe for database use
$q = mysql_query("SELECT * FROM infor WHERE pageID='$id'");
}
//get page data from database and create an object
$r = mysql_fetch_object($q);
//print the pages content
echo "<h1>$r->pageTitle</h2>";
echo "$r->pageForum<br>";
echo "$r->imgLink<br>";
?>
<?php
if(isset($_POST['submit'])){
$row1 = $_POST['username'];
$row1 = mysql_real_escape_string($row1);
mysql_query("UPDATE members SET isMcount = isMcount + 1 WHERE username = '".$row1."' ") or die(mysql_error());
//THIS ONE DON'T WORK
mysql_query("UPDATE infor INNER JOIN (select min(isCount) as min_is_cnt from infor ) m SET isCount = isCount + 1 WHERE isCount=m.min_is_cnt LIMIT 1") or die(mysql_error());
$_SESSION['success'] = 'Page Updated';
header('Location: '.DIRADMIN);
exit();
}
?>
<form method="post">
<select name="username">
<?php
$sql = mysql_query("SELECT username FROM members");
while ($row1 = mysql_fetch_array($sql)){
echo "<option value=\"" . $row1['username'] . "\">" . $row1['username'] . "</option>";
}
?>
</select>
<input type="submit" name="submit" value="Submit" class="button" />
</form>
<?php
答案 0 :(得分:1)
尝试将您的查询更改为:
mysql_query("UPDATE infor SET isCount = isCount + 1 WHERE isCount=(SELECT min(isCount) FROM (SELECT * FROM infor) b) LIMIT 1") or die(mysql_error());
检查You can't specify target table for update in FROM clause,了解在内部查询中执行SELECT *
的原因。