Codeigniter concat and join
I want to concat 2 fields together in my codeigniter db query but its not working, can you help me please?
$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');
Thanks
At a minimum, you're missing a ->from
. This means you have nothing to join to.
The code you posted is the complete one? Try adding the get function:
$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');
Do you get any error? You could also try printing the query with:
print_r( $this->db->last_query() );
if you are using CONCAT you need to pass FALSE as an argument after your select statement. Like so:
$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');
see this answer: concat in php codeigniter
精彩评论