简体   繁体   English

Codeigniter按时间排序-查询生成

[英]Codeigniter order by time - query generation

I actually need a mysql query like 我实际上需要像这样的mysql查询

SELECT *, CONCAT(pri_hours, ':', pri_minutes, ' ', IF(pri_ampm = 1, 'am', 'pm')) as hr FROM (`sms_primary`)  ORDER BY STR_TO_DATE(hr, '%h:%i %p')

and as I use codeigniter i wrote query as below 当我使用codeigniter时,我编写了如下查询

       $this->db->select("*,CONCAT(pri_hours,':', pri_minutes,' ',IF(pri_ampm = 1,'am','pm')) as hr",FALSE);
       $this->db->from('sms_primary');
       $this->db->order_by("STR_TO_DATE(hr,'%h:%i %p')");

But i am not getting the expected query i get it as below 但是我没有得到预期的查询,我得到如下

SELECT *, CONCAT(pri_hours, ':', pri_minutes, ' ', IF(pri_ampm = 1, 'am', 'pm')) as hr FROM (`sms_primary`) ORDER BY STR_TO_DATE(hr, `'%h:%i` %p') 

I hope you spot out the difference in the generated query. 我希望您能发现所生成查询中的差异。 Its a injection off some unwanted operator `. 它注入了一些不需要的运算符`。 I just want to remove it.How to do that? 我只想删除它,该怎么做?

Change 更改

STR_TO_DATE(hr, `'%h:%i` %p') 

to

 STR_TO_DATE(hr, '%h:%i %p')

codeigniter is adding backticks. Codeigniter正在添加反引号。 use this before your query 在查询之前使用它

$this->db->_protect_identifiers=false; 

hope this helps 希望这可以帮助

You require to use this->db->_protect_identifiers=false; 您需要使用this-> db-> _ protect_identifiers = false;

In your query,it should be like : 在您的查询中,它应类似于:

$this->db->_protect_identifiers = FALSE; // Make it false here $this->db->order_by("STR_TO_DATE(hr,'%h:%i %p')");
$this->db->_protect_identifiers = TRUE; // Make it true again when order_by code completed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM