$ this-> db->选择(" DATE_FORMAT(q.added,'%d%b%y')添加")在sql语句中返回错误

时间:2015-03-20 01:44:15

标签: php mysql codeigniter

我正在使用Codeigniter v2.2.0。 我创建了自己的db帮助器。

db_select = $this->db->select
db_join = $this->db->join
db_where = $this->db->where
db_get = $this->db->get
db_query = $this->db->query

现在,这是我的意见:

    $mb_no = $this->curuser['admin_id'];

    db_select('q.question_id');
    db_select('obj.question_bm as obj_question_bm');
    db_select('obj.question_en as obj_question_en');
    db_select('st.question_bm as st_question_bm');
    db_select('st.question_en as st_question_en');
    db_select('q.format as q_format');
    db_select("DATE_FORMAT(q.added,'%d %b %y') as added");
    db_join('objectives obj','obj.question_id=q.question_id','left');
    db_join('structures st','st.question_id=q.question_id','left');
    db_where('q.admin_id',$mb_no);

我在DATE_FORMAT

时收到了sql错误
db_select("DATE_FORMAT(q.added,'%d %b %y') as added");

这是错误:

Error Number: 1054
Unknown column '%d' in 'field list'

当我使用db_query($ this-> db-> query())时。它不会返回错误; 这是我使用db_query的第二个sql语句:

db_query("SELECT `q`.`question_id`,`obj`.`question_bm` as obj_question_bm, `obj`.`question_en` as obj_question_en, `st`.`question_bm` as st_question_bm, `st`.`question_en` as st_question_en, `q`.`format` as q_format, DATE_FORMAT(q.added,'%b') as added FROM (`questions` q) LEFT JOIN `objectives` obj ON `obj`.`question_id`=`q`.`question_id` LEFT JOIN `structures` st ON `st`.`question_id`=`q`.`question_id` WHERE `q`.`admin_id` = '1'");

如何使用

使用db_select获取日期格式
DATE_FORMAT(q.added,'%d %b %y') as added

而不是使用db_query()?

1 个答案:

答案 0 :(得分:1)

使用此

db_select("DATE_FORMAT(q.added,'%d %b %y') as added",false);