gpt4 book ai didi

php - 将 SQL 查询转换为 CodeIgniter 事件记录

转载 作者:搜寻专家 更新时间:2023-10-31 22:00:23 24 4
gpt4 key购买 nike

我只想查询那些点赞数最高的项目。

这是我想转换成 CodeIgnitor 的事件记录的 sql 查询:

SELECT *, SUM(like) as totalLikes
FROM tbl_like
GROUP BY uploadID
ORDER BY totalLikes DESC
LIMIT 2

CodeIgniter:

public function get_cheezyPic(){
$this->db->select('uploadID, SUM(like) as totalLikes');
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);

$query= $this->db->get();

return $query->result_array();}

但是当我尝试运行这段代码时,我遇到了这个错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like) as totalLikes FROM (tbl_like) GROUP BY uploadID ORDER BY totalLikes ' at line 1

SELECT `uploadID`, SUM(like) as totalLikes FROM (`tbl_like`) GROUP BY `uploadID` ORDER BY `totalLikes` desc LIMIT 2

这段代码有什么问题?

感谢您的帮助。

最佳答案

这对你有用。请注意,您使用的是保留关键字,因此它应该包含在 backtic `` 中。

public function get_cheezyPic(){
$this->db->select('uploadID, SUM(`like`) as totalLikes',false);
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);

$query= $this->db->get();

return $query->result_array();
}

关于php - 将 SQL 查询转换为 CodeIgniter 事件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30092175/

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