CONCAT() function in a SELECT field list

二次信任 提交于 2019-12-11 00:00:53

问题


I'm using CodeIgniter's active record features but I'm not able to select the data I'm interested in.

What I want to be selected is:

CONCAT(t.field1, ' / ', t.field2) AS `finalValue`

So I add this:

$this->db->select('CONCAT(t.field1, \' / \', t.field2) AS `finalValue`');

But this is the query string that's generated:

CONCAT(t.field1, `'` / ', `t`.`field2)` AS `finalValue`

Is this a bug? Am I specifying it incorrectly?


回答1:


You can actually turn off the default escaping mechanism, which is the source of a strange issue when using MySQL functions, by passing FALSE as the second parameter of 'select' method.

Be aware that you must then handle escaping yourself if you do this.

Oh, and you could use double quotes to reduce all that escaping you have going on.




回答2:


do this instead?

$this->db->select("CONCAT(t.field1, ' / ', t.field2) AS `finalValue`");

I.E Double quotes round the outside to avoid the need for unsightly escaping



来源:https://stackoverflow.com/questions/4519838/concat-function-in-a-select-field-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!