gpt4 book ai didi

mysql - CodeIgniter Datamapper ORM 是否支持 ORDER BY 子句中的 MySQL CASE 语句?

转载 作者:行者123 更新时间:2023-12-01 00:27:34 25 4
gpt4 key购买 nike

将 Datamapper 1.8.1-dev 用于 PHP 的 CodeIgniter,我正在尝试构建此查询以在列表顶部优先考虑一个国家名称:

SELECT * FROM countries
ORDER BY CASE name WHEN 'Australia' THEN 1 ELSE 2 END, name

My Country 模型(扩展 Datamapper)上的 PHP:

$countries = new Country();
$countries->order_by("CASE name WHEN 'Australia' THEN 1 ELSE 2 END, name");
$countries->get_iterated();

Datamapper 改为将“CASE”解析为表名,构建此语法错误查询:

SELECT * FROM (`countries`)
ORDER BY `CASE` name WHEN 'Australia' THEN 1 ELSE 2 END, `countries`.`name`

我假设 CASE 案例没有被处理,因为这个流控制语句刚刚添加到 MySQL 5 中?我唯一的选择是向 countries 表添加 order_by 列还是在数据库检索后使用 PHP 排序函数?

最佳答案

我能够在 order by 中使用 case 语句。您只需要将 case 语句括在括号中即可。如果您不这样做,单词 case 将被视为列名并反引号。

$this->db->distinct(); 
$this->db->select('country');
$this->db->from('distributors');
$this->db->where('country != ', '');
$this->db->order_by("(CASE country WHEN 'UNITED STATES OF AMERICA' THEN '1' ELSE country END)");

关于mysql - CodeIgniter Datamapper ORM 是否支持 ORDER BY 子句中的 MySQL CASE 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11821503/

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