我尝试这样做时出错:
$ci =& get_instance();
$ci->db->select("CONCAT_WS(' ',users.name_first,users.name_last) AS user_name,CONCAT_WS(' ',advertisers.name_first,advertisers.name_last) AS advertiser_name,image,advertiser_reviews.last_updated");
$ci->db->join('users', 'users.id = advertiser_reviews.user_id');
$ci->db->join('advertisers', 'advertisers.id = advertiser_reviews.advertiser_id');
$ci->db->order_by('advertiser_reviews.id','desc');
$ci->db->limit(1);
$query = $ci->db->get('advertiser_reviews');
错误如下:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (`hc_advertiser_reviews`) JOIN `hc_users` ON `hc_users`.`id` = `hc_advertis' at line 2
SELECT CONCAT_WS(' ', `hc_users`.`name_first`, `hc_users`.`name_last)` AS user_name,
CONCAT_WS(' ', `hc_advertisers`.`name_first`, `hc_advertisers`.`name_last)` AS advertiser_name,
`image`,
`hc_advertiser_reviews`.`last_updated`
FROM (`hc_advertiser_reviews`)
JOIN `hc_users` ON `hc_users`.`id` = `hc_advertiser_reviews`.`user_id`
JOIN `hc_advertisers` ON `hc_advertisers`.`id` = `hc_advertiser_reviews`.`advertiser_id`
ORDER BY `hc_advertiser_reviews`.`id` desc LIMIT 1
答案 0 :(得分:6)
尝试使用$this->db->query("your sql query")
编辑: 尝试使用
来避免CodeIgniter DB类的自动引用功能$this->db->select("your-query",FALSE)
答案 1 :(得分:0)
在Codeigniter生成的代码末尾有符号`
SELECT CONCAT_WS(' ', `hc_users`.`name_first`, `hc_users`.`name_last)`
答案 2 :(得分:0)
$this->db->select('what of sql uncion' ,false);
虚假告诉ci忽略用`
逃避答案 3 :(得分:0)
刚刚发现这篇文章,我遇到了同样的问题,你可以通过巧妙地使用空格来解决这个问题。
$ci->db->select("CONCAT_WS(' ',users.name_first,users.name_last ) AS user_name )
与
相反$ci->db->select("CONCAT_WS(' ',users.name_first,users.name_last) AS user_name )
如果在CONCAT_WS函数的最后一个右括号之前插入一个空格,那么CodeIgniter自动引用功能就不会混淆需要去的地方。这也意味着您不必使用db-> query()函数。