CodeIgniter GROUP_CONCAT并加入

Cla*_*ont 6 php mysql codeigniter

我试图找到一种方法将这两个表连接在一起,我能够做到,但如果找到多个匹配的值,它会再次显示产品表中的所有内容.现在我试图将MySQL group_concat一起使用,以便能够在数组中的一个字段中列出所有tName但是我一直在使用MySQL收到错误:

错误号码:1064

您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第2行的'FROM(sp_product)LEFT OUTER JOIN sp_product_typeON sp_product_type.`tCat' 附近使用正确的语法

选择sp_product.name,sp_product.price,sp_product.perm_name,sp_product.description,GROUP_CONCAT(product_type.tName SEPARATOR FROM(sp_product)LEFT OUTER JOIN sp_product_typeON sp_product_type.tCategory= sp_product.typeWHERE perm_name='bacon'

$this->db->select('product.name, product.price, product.perm_name, product.description, GROUP_CONCAT(product_type.tName SEPARATOR ',') as product_type.tName'); 
$this->db->from('product');
$this->db->where('perm_name', $this->uri->segment(2));
$this->db->join('product_type', 'product_type.tCategory = product.type', 'LEFT OUTER');
$query = $this->db->get(); 
Run Code Online (Sandbox Code Playgroud)

我有什么想法我做错了吗?

小智 7

似乎报价不当引起问题.

它应该是 GROUP_CONCAT(product_type.tName SEPARATOR ",")

试试以下:

$this->db->select('product.name, product.price, product.perm_name, product.description, GROUP_CONCAT(product_type.tName SEPARATOR ",") as product_type.tName'); 
    $this->db->from('product');
    $this->db->where('perm_name', $this->uri->segment(2));
    $this->db->join('product_type', 'product_type.tCategory = product.type', 'LEFT OUTER');
    $query = $this->db->get(); 
Run Code Online (Sandbox Code Playgroud)