[英]how to get max value of a sql database query in php
I want to find out the max value of sql data column on the basis of email id and username. 我想根据电子邮件ID和用户名找出sql数据列的最大值。 (there is no primary entity) To get the email id, i store the user email id from session. (没有主要实体)要获取电子邮件ID,我存储了会话中的用户电子邮件ID。
here goes my code: 这是我的代码:
$emailid = $userRow['emailid'];
$sql = "select max(item) from product where email = '$emailid' AND username = 'arun'";
$result = $conn-> query($sql);
$row = $result->fetch_assoc();
echo "Max item : " .$row['result'];
It's giving me first value of sql table but not highest. 它给了我sql表的第一个值,但不是最高值。
you can either change your column data-type or use CAST
or CONVERT
. 您可以更改列数据类型,也可以使用CAST
或CONVERT
。
$sql = "SELECT MAX( CAST( `item` AS UNSIGNED) ) as max FROM `product` WHERE `email` = '$emailid' AND `username` = 'arun'";
If possible its better to change the data-type. 如果可能的话,最好更改数据类型。
Try this, 尝试这个,
$emailid = $userRow['emailid'];
$sql = "SELECT MAX(item) AS item FROM product WHERE email = '$emailid' AND username = 'arun'";
$result = $conn-> query($sql);
$row = $result->fetch_assoc();
echo "Max item : " .$row['item'];
Try this way, 试试这个
$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.