gpt4 book ai didi

php - Mysql Max 函数不显示多条记录

转载 作者:行者123 更新时间:2023-11-29 15:13:29 25 4
gpt4 key购买 nike

我有两个表“users”和“paymenthistory”,我想获取具有最高“bookingid”的特定“user”的记录,但它只向我显示一条记录,应该显示 2 条记录,“max”函数有问题,这是我的表格

表“付款”

id  bookingid   userid
1 142 3
2 146 2
3 148 3
4 154 5

表“用户”

id      name        
1 abc
2 zyd
3 xyz
4 nwd

这是我的查询

$this->db->select('p.bookingId,MAX(p.bookingId) as lastBookingId,p.userid,u.name');
$this->db->from('payment p');
$this->db->join('users u','p.userid=u.id');
$this->db->where('p.user',"3");

显示一条记录,但我想获取同一列“lastbookingid”(相同)的 2 条记录,我错在哪里?

最佳答案

我从您在评论中的回复中了解到了您的观点。这是更新后的 SQL 语句,它将产生您想要的结果

SELECT p.bookingId,
(SELECT MAX(x.bookingId) FROM payment x WHERE x.userid = p.userid)
AS lastBookingId,
p.userid, u.name
FROM payment p
LEFT JOIN users u ON p.userid = u.id
WHERE p.userid = 3;

使用内部SQL语句,避免由于使用聚合函数(max)而隐式添加“group by”导致结果只显示一行。

这是测试的输出:

enter image description here

关于php - Mysql Max 函数不显示多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59876460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com