如何在 codeIgniter (mysql) 中按 'name + 0' 排序
How to ORDER BY 'name + 0' in codeIgniter (mysql)
我在 MySQL 数据库中有一个 table,如下所示:
id | name
1 | 1 some words
2 | 2 some other words
3 | 1.1 some other words
...
10 | 10 some other words
如果我使用以下方法对 table 进行排序:
$this->db->select('*')
->order_by('name', 'ASC')
->get('table_name');
我按以下顺序收到 table:
id | name
1 | 1 some words
3 | 1.1 some other words
10 | 10 some other words
...
2 | 2 some other words
但我实际上想按以下顺序接收 table:
id | name
1 | 1 some words
3 | 1.1 some other words
2 | 2 some other words
...
10 | 10 some other words
这可以使用以下 SQL 语句:
SELECT * FROM database_name.table_name ORDER BY name + 0 ASC;
但是如果我像这样使用 codeIgniters 查询生成器,我会收到一个数据库错误:
$this->db->select('*')
->order_by('name + 0', 'ASC')
->get('table_name');
请注意,在我的情况下,不可能将数字存储在不同的列中或按 ID 排序。
那么有没有办法让这个 SQL 语句在 CodeIgniters 查询生成器中工作?
SELECT * FROM database_name.table_name ORDER BY name + 0 ASC;
提前致谢
编辑:
对于混淆,我感到非常抱歉,但 '.'在 1.1 中并不意味着是一个浮点数,而是像一个点:1.1.1、1.1.2、1.1.3
我找到了一个使用@Marc B 解决方案的解决方案,并将其放入查询构建器中,如下所示:
$query = $this->db->select('name+0 AS name', FALSE)
->order_by('name', 'ASC')
->get('table_name');
非常感谢大家的回答
使用派生字段和别名?
SELECT name+0 AS fakename ...
ORDER BY fakename
我认为你应该先按数字再按文字订购。
示范:
SET @str := '1.1 some other words';
SELECT
SUBSTRING_INDEX(@str,' ',1)+0 AS numberPart,
SUBSTRING_INDEX(@str,SUBSTRING_INDEX(@str,' ',1),-1) AS textPart;
输出:
numberPart textPart
1.1 some other words
完整查询如下:
SELECT
*
FROM database_name.table_name
ORDER BY SUBSTRING_INDEX(name,' ',1)+0,
SUBSTRING_INDEX(name,SUBSTRING_INDEX(name,' ',1),-1);
或者您可以尝试将数字字符串转换为十进制类型。
试试这个:
$this->db->select('*')
->order_by('CAST(name AS DECIMAL(10,6)) ASC')
->get('table_name');
提取第一个字符串(在@1000111's example之后)并CAST
它到DECIMAL
有一个小数位:
SELECT id
, name
FROM table_name
ORDER BY CAST(SUBSTRING_INDEX(name,' ',1) AS DECIMAL (8,1))
;
在 CodeIgniter 中,它看起来像这样:
$this->db->select('*')
->order_by('CAST(SUBSTRING_INDEX(name,' ',1) AS DECIMAL (8,1))', 'ASC')
->get('table_name');
如果您的标题有更多小数位,请更改 DECIMAL 定义。例如,如果您有 1.15 和 1.17,那么您需要保留两位小数,因此您可以将其更改为 DECIMAL(9, 2).
使用
$this->db->order_by('fieldname + 0 ','',false,false);
这对我适用于 Codeigniter 版本 3
$this->db->select('*');
$this->db->order_by('sum(fieldname)', 'ASC');
$this->db->order_by('CAST(fieldname AS SIGNED) ASC');
我在 MySQL 数据库中有一个 table,如下所示:
id | name
1 | 1 some words
2 | 2 some other words
3 | 1.1 some other words
...
10 | 10 some other words
如果我使用以下方法对 table 进行排序:
$this->db->select('*')
->order_by('name', 'ASC')
->get('table_name');
我按以下顺序收到 table:
id | name
1 | 1 some words
3 | 1.1 some other words
10 | 10 some other words
...
2 | 2 some other words
但我实际上想按以下顺序接收 table:
id | name
1 | 1 some words
3 | 1.1 some other words
2 | 2 some other words
...
10 | 10 some other words
这可以使用以下 SQL 语句:
SELECT * FROM database_name.table_name ORDER BY name + 0 ASC;
但是如果我像这样使用 codeIgniters 查询生成器,我会收到一个数据库错误:
$this->db->select('*')
->order_by('name + 0', 'ASC')
->get('table_name');
请注意,在我的情况下,不可能将数字存储在不同的列中或按 ID 排序。
那么有没有办法让这个 SQL 语句在 CodeIgniters 查询生成器中工作?
SELECT * FROM database_name.table_name ORDER BY name + 0 ASC;
提前致谢
编辑: 对于混淆,我感到非常抱歉,但 '.'在 1.1 中并不意味着是一个浮点数,而是像一个点:1.1.1、1.1.2、1.1.3 我找到了一个使用@Marc B 解决方案的解决方案,并将其放入查询构建器中,如下所示:
$query = $this->db->select('name+0 AS name', FALSE)
->order_by('name', 'ASC')
->get('table_name');
非常感谢大家的回答
使用派生字段和别名?
SELECT name+0 AS fakename ...
ORDER BY fakename
我认为你应该先按数字再按文字订购。
示范:
SET @str := '1.1 some other words';
SELECT
SUBSTRING_INDEX(@str,' ',1)+0 AS numberPart,
SUBSTRING_INDEX(@str,SUBSTRING_INDEX(@str,' ',1),-1) AS textPart;
输出:
numberPart textPart
1.1 some other words
完整查询如下:
SELECT
*
FROM database_name.table_name
ORDER BY SUBSTRING_INDEX(name,' ',1)+0,
SUBSTRING_INDEX(name,SUBSTRING_INDEX(name,' ',1),-1);
或者您可以尝试将数字字符串转换为十进制类型。
试试这个:
$this->db->select('*')
->order_by('CAST(name AS DECIMAL(10,6)) ASC')
->get('table_name');
提取第一个字符串(在@1000111's example之后)并CAST
它到DECIMAL
有一个小数位:
SELECT id
, name
FROM table_name
ORDER BY CAST(SUBSTRING_INDEX(name,' ',1) AS DECIMAL (8,1))
;
在 CodeIgniter 中,它看起来像这样:
$this->db->select('*')
->order_by('CAST(SUBSTRING_INDEX(name,' ',1) AS DECIMAL (8,1))', 'ASC')
->get('table_name');
如果您的标题有更多小数位,请更改 DECIMAL 定义。例如,如果您有 1.15 和 1.17,那么您需要保留两位小数,因此您可以将其更改为 DECIMAL(9, 2).
使用
$this->db->order_by('fieldname + 0 ','',false,false);
这对我适用于 Codeigniter 版本 3
$this->db->select('*');
$this->db->order_by('sum(fieldname)', 'ASC');
$this->db->order_by('CAST(fieldname AS SIGNED) ASC');