简体   繁体   中英

UPDATE first column with min value LIMIT 1 with INNER JOIN, in PHP

I need the following to UPDATE the row that have the min value and it have to be limit to 1 right now it will UPDATE all the column that have the min value if its the same I tried to put in the LIMIT 1 but I keep getting an error. (the reason for the workaround "INNER JOIN" its because of the table is already in use so I can't update it without.

See the comment pointing out the error: //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

Try changing your query to:

mysql_query("UPDATE infor SET isCount = isCount + 1 WHERE isCount=(SELECT min(isCount) FROM (SELECT * FROM infor) b) LIMIT 1") or die(mysql_error());

Check You can't specify target table for update in FROM clause for the reasoning behind doing a SELECT * in the inner query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM