简体   繁体   English

将列合并为一列mysql

[英]combine columns into one column mysql

I would like to combine two columns in one column as Fullname, and for that I have written the following code: 我想将两列合并为全名,为此,我编写了以下代码:

    $this->db->select('CONCAT(first_name," ",last_name) AS FullName');
    $this->db->from('customer');
    $this->db->where('user_id',1);
    $query = $this->db->get();
    return $query->result_array();

The resulting query would be: 结果查询为:

SELECT CONCAT(first_name," ",last_name) AS FullName 
FROM customer 
WHERE user_id = 1

but when i execute the above code it gives me: 但是当我执行上面的代码时,它给了我:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ( customer ) WHERE user_id = '1' at line 2 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第2行的'FROM( customer )WHERE user_id ='1'附近使用

I have also tried with concat_ws group_concat but not able to get it work. 我也尝试使用concat_ws group_concat但无法使其正常工作。 Can anyone see what I'm doing wrong? 谁能看到我在做什么错?

By default, CI tries to escape what you pass to db->select() (in case you were passing in user-generated values). 默认情况下,CI尝试转义您传递给db->select() (以防您传递用户生成的值)。 You can disable this feature by passing false as a second argument. 您可以通过传递false作为第二个参数来禁用此功能。

$this->db->select('CONCAT(first_name," ",last_name) AS FullName', false);

I have been through this before with CI, in my case CI was wrongly creating the query, for example putting simgle quotes where they shouldn't be. 在使用CI之前,我已经经历过此过程,在我的情况下,CI错误地创建了查询,例如将单引号放在不应有的地方。

My advice create the query yourself and run it, you could be surprise :P 我的建议是自己创建查询并运行它,您可能会感到惊讶:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM