SELECT *, SUM(`Relaxation_rating` + `Food_rating`) AS Ratings
GROUP BY CODE
ORDER BY Ratings DESC
FROM destinations
如何在 CodeIgniter 中将此 sql 代码转换为 CctiveRecord?
我尝试了以下方法,但确实没有结果。
$this->db->select("'(SELECT *, SUM(Relaxation_rating + Food_rating) FROM database GROUP BY CODE') AS Ratings", FALSE);
$this->db->get("database");
尝试:
$data = $this->db->select('*, SUM(`Relaxation_rating` + `Food_rating`) AS Ratings', false)->group_by('CODE')->order_by('Ratings', 'desc')->get('destinations')->result_array();
或者,
$this->db->select('*, SUM(`Relaxation_rating` + `Food_rating`) AS Ratings', false)->group_by('CODE')->order_by('Ratings', 'desc')->get('destinations')->result();
尝试一下
$this->db->select('*, SUM(`Relaxation_rating` + `Food_rating`) AS Ratings');
$this->db->from('destinations');
$this->db->group_by('CODE');
$this->db->order_by('Ratings','desc');
$result = $this->db->get();
工作查询构建器方法脚本不显式注入特定于方言的标识符引号(例如反引号或双引号)。
SELECT 子句中没有引号:
SELECT *, SUM(relaxation_rating + food_rating) ratings
FROM `database`
GROUP BY `code`
ORDER BY `ratings` DESC
生成者:
$this->db
->select('*, SUM(relaxation_rating + food_rating) ratings')
->group_by('code')
->order_by('ratings', 'DESC')
->get('database')
->result();
或
$this->db
->select()
->select('SUM(relaxation_rating + food_rating) ratings')
->group_by('code')
->order_by('ratings', 'DESC')
->get('database')
->result();
通过破解受保护的
_protect_identifiers
属性,在 SELECT 子句中的列上没有引号:
SELECT *, SUM(relaxation_rating + food_rating) ratings
FROM `database`
GROUP BY `code`
ORDER BY `ratings` DESC
生成者:
$this->db->select();
(fn() => $this->_protect_identifiers = false)->call($this->db);
$this->db->select_sum('relaxation_rating + food_rating', 'ratings');
(fn() => $this->_protect_identifiers = true)->call($this->db);
return $this->db
->group_by('code')
->order_by('ratings', 'DESC')
->get('database')
->result();
黑客保护类属性的另一个演示可以在如何列出 CodeIgniter 应用程序中所有加载的库和帮助程序?
对 SELECT 子句中所有列/别名的引用:
SELECT *, SUM(`relaxation_rating` + `food_rating`) `ratings`
FROM `database`
GROUP BY `code`
ORDER BY `ratings` DESC
生成者:
$this->db
->select(
sprintf(
'*, SUM(%s + %s) %s',
$this->db->escape_identifiers('relaxation_rating'),
$this->db->escape_identifiers('food_rating'),
$this->db->escape_identifiers('ratings')
),
false
)
->group_by('code')
->order_by('ratings', 'DESC')
->get('database')
->result();