Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在Codeigniter中使用查询生成器-如何正确使用别名?_Mysql_Sql_Codeigniter - Fatal编程技术网

Mysql 在Codeigniter中使用查询生成器-如何正确使用别名?

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

我使用codeigniter 3.x中的查询生成器构建了以下查询

        $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