[英]How to select max date?
My code is我的代码是
function lister($a,$con){
$queryq = " SELECT * FROM note WHERE date= MAX(date) AND id='".$a."' AND admin='".$_SESSION['login_username']."' ";
$resultq = mysqli_query($con,$queryq) or die('Error: ' . mysqli_error($con));
$arrayq=mysqli_fetch_array($resultq,MYSQL_NUM); {
echo $arrayq[4];
}
}
There is an error:有一个错误:
Error: Invalid use of group function
错误:无效使用组功能
What am I doing wrong?我究竟做错了什么?
try to split the query尝试拆分查询
$queryA = "SELECT date FROM `note` WHERE id='".$a."' AND admin='".$_SESSION['login_username']."' ORDER BY date DESC LIMIT 1";
$resultA = mysql_query($con, $queryA);
$maxDatePerUser = mysql_fetch_array($resultA);
$queryq = " SELECT * FROM note WHERE id='".$a."' AND admin='".$_SESSION['login_username']."' AND date='" . $maxDatePerUser["date"] ."'";
$resultq = mysqli_query($con,$queryq) or die('Error: ' . mysqli_error($con));
$arrayq=mysqli_fetch_array($resultq,MYSQL_NUM); {
The problem is caused by a circular reference of sorts.该问题是由各种循环引用引起的。
SELECT * FROM note WHERE date= MAX(date) AND id='".$a."' AND admin='".$_SESSION['login_username']."' "
You're asking the function to calculate something based on that same thing - a condition of the statement is MAX(date), but it can't calculate that until it knows what it is.您要求函数根据相同的内容计算某些内容 - 语句的条件是 MAX(date),但在知道它是什么之前无法计算它。
The best solution is to find MAX(date) separately:最好的解决方案是单独找到 MAX(date):
SELECT max(date) FROM note
You can add additional restrictions, if necessary, if it is the max date WHERE, ie如有必要,您可以添加其他限制,如果它是 WHERE 的最大日期,即
SELECT max(date) FROM note WHERE id='".$a."' AND admin='".$_SESSION['login_username']."' "
Once you have this, you can pop it back in the query:一旦你有了这个,你可以在查询中弹出它:
SELECT * FROM note
WHERE date = (SELECT max(date)
FROM note
WHERE id = '" . $a . "'
AND admin = '" . $_SESSION['login_username'] . "')
AND id = '" . $a . "'
AND admin = '" . $_SESSION['login_username'] . "';
Hard to get the query 100% right without having the data to test on, but that should work.如果没有要测试的数据,很难 100% 正确地进行查询,但这应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.