简体   繁体   English

从mysql表中选择行和最大值

[英]select row and max value from mysql table

$query = mysql_query("SELECT * 
                     FROM farm_flocks 
                     WHERE Client_Id='" .$_SESSION["id"] . "' 
                     THEN 
                     SELECT Flock_id in (
                           select max(Flock_id) from 
                           farm_flocks
                     )", $connection);

        if (mysql_num_rows($query) == 1) {

            $row = mysql_fetch_assoc($query);

            //do some stuff

        }

Trying to select all the rows that have the same client Id then after that narrow it the selection down to 1 row by selecting the highest value from the Flock_id column. 尝试选择具有相同客户端ID的所有行,然后通过从Flock_id列中选择最大值,将选择范围缩小到1行。

Change your query as follows. 如下更改查询。

$query = mysql_query("SELECT *, max(Flock_id) AS Max_Flock_id FROM farm_flocks WHERE Client_Id='" .$_SESSION["id"] . "')", $connection);

Also, mysql_* is deprecated as of PHP 5.5.0, and will be removed in the future. 另外,从PHP 5.5.0开始不推荐使用mysql_* ,以后将删除它。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLiPDO_MySQL扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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