简体   繁体   中英

How to properly use mysql MONTH() and YEAR() in codeigniter active record

I'm using Codeigniter Active Record Class trying to get the month and year part of a column wherein the dataType is DATE

$this->db->where('mo', 'MONTH(so_date)');
$this->db->where('yr', 'YEAR(so_date)');

Code above gives me this. The MONTH() AND YEAR() are enclosed within single quotation marks.

SELECT *
FROM mytbl 
WHERE `mo` = 'MONTH(so_date)'
AND `yr` = 'YEAR(so_date)'

//so_date looks like this : 2013-04-15

What am I doing wrong here or what do I change so as not to get the quotation marks? When I remove the quotes and try to run it in PHPMYADMIN the query gives me exactly what I want.

from doc

$this->db->where() accepts an optional third parameter.

If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks .

$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);

so do something like :

$this->db->where('mo', 'MONTH(so_date)' , FALSE);
$this->db->where('mo = MONTH(so_date)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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