esandrkwn
esandrkwn

Reputation: 399

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.

Upvotes: 1

Views: 8767

Answers (2)

JOE LEE
JOE LEE

Reputation: 1058

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

Upvotes: 1

Haim Evgi
Haim Evgi

Reputation: 125564

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);

Upvotes: 1

Related Questions