Mysql 在Codeigniter中使用查询生成器-如何正确使用别名?
我使用codeigniter 3.x中的查询生成器构建了以下查询Mysql 在Codeigniter中使用查询生成器-如何正确使用别名?,mysql,sql,codeigniter,Mysql,Sql,Codeigniter,我使用codeigniter 3.x中的查询生成器构建了以下查询 $this->db->select(' categories.*, posts.id, posts.category_id, posts.user_id, posts.updated_user_id, posts.title, posts
$this->db->select('
categories.*,
posts.id,
posts.category_id,
posts.user_id,
posts.updated_user_id,
posts.title,
posts.slug,
posts.body,
posts.post_image,
year(posts.created_timestamp) as year,
month(posts.created_timestamp) as month
');
上述操作会在“where子句”中生成错误的未知列“year”。我已经转储了sql,但无法理解为什么sql不能识别年份和月份
SELECT
`categories`.*,
`posts`.`id`,
`posts`.`category_id`,
`posts`.`user_id`,
`posts`.`updated_user_id`,
`posts`.`title`,
`posts`.`slug`,
`posts`.`body`,
`posts`.`post_image`,
year(posts.created_timestamp) as year,
month(posts.created_timestamp) as month
FROM `posts`
JOIN `categories` ON `categories`.`id` = `posts`.`category_id`
WHERE
`year` = '2019'
AND `month` = '1'
LIMIT 5
这不是codeigniter的问题,MySQL的工作原理不是字段名(请避免使用这种字段名) 它是一个函数,选择或调用函数与定义函数不同,所以必须以选择该函数的相同方式放置where子句
SELECT
`categories`.*,
`posts`.`id`,
`posts`.`category_id`,
`posts`.`user_id`,
`posts`.`updated_user_id`,
`posts`.`title`,
`posts`.`slug`,
`posts`.`body`,
`posts`.`post_image`,
year(posts.created_timestamp) as year,
month(posts.created_timestamp) as month
FROM `posts`
JOIN `categories` ON `categories`.`id` = `posts`.`category_id`
WHERE
year(posts.created_timestamp) = '2019'
AND month(posts.created_timestamp) = '1'
LIMIT 5