简体   繁体   English

从MySQL表中选择最大编号无效

[英]Selecting MAX number from MySQL table not working

I've looked this up and I've yet to figure this out. 我已经看过了,但还没有弄清楚。 This is part of my table. 这是我桌子的一部分

What I'm trying to do is, grab the biggest number in the tradeNum column. 我想做的是在tradeNum栏中获取最大的数量。 What I have so far is: 到目前为止,我有:

$sql = "SELECT MAX(tradeNum) FROM trades";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "num: " . $row["tradeNum"] . "<br>";
    }
} else {
    echo "0 results";
}

While this doesn't echo "0 results," it echos nothing. 虽然这不会回显“ 0结果”,但它不会回显任何内容。 I'm still new to mysql, but shouldn't this work? 我还是mysql的新手,但这不行吗? I took most of the code from here if it helps. 如果有帮助,我从这里获取了大部分代码。 Sorry if I'm vague, let me know if I need to clear something up. 抱歉,如果我不确定,请告诉我是否需要清除一些内容。

您查询的应该是

$sql = "SELECT MAX(tradeNum) AS tradeNum FROM trades";
$sql = "SELECT MAX(tradeNum) tradeNum FROM trades"; // here use tradeNum as alias
$result = $conn->query($sql);
echo $result[0]['tradeNum'];

由于您使用的是tradeNum列来获取数据,因此您还可以使用带有limit order by

SELECT tradeNum FROM trades order by tradeNum desc limit 1

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

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