我想在我的codeigniter数据库查询中将两个字段连在一起,但它不起作用,你能帮我吗?
$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author');
$this->db->join('users', 'users.id = news.author', 'left');
$this->db->join('users modified_user', 'modified_user.id = news.modified_by', 'left');
由于
答案 0 :(得分:3)
至少,你错过了->from
。这意味着您无需加入。
答案 1 :(得分:0)
您发布的代码是完整的代码吗?尝试添加get函数:
$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author')->join('users', 'users.id = news.author', 'left')->join('users modified_user', 'modified_user.id = news.modified_by', 'left');
$this->db->get('TableName');
你有任何错误吗?您也可以尝试使用以下方法打印查询:
print_r( $this->db->last_query() );
答案 2 :(得分:0)
如果您正在使用CONCAT,则需要在select语句之后将FALSE作为参数传递。像这样:
$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author',FALSE);
$ this-> db-> join('users','users.id = news.author','left');
$ this-> db-> join('users modified_user','modified_user.id = news.modified_by','left');
看到这个答案: concat in php codeigniter