2012-02-17 106 views
2

我想找到一种方法将这两个表连接起来,这是我能够做到的,但是如果它发现多个匹配的值,它会再次显示产品表中的所有内容。现在我试图使用MySQL GROUP_CONCAT在一起,能够列出所有TNAME在一个领域的阵列中,但我不断收到一个错误与MySQL:CodeIgniter GROUP_CONCAT并加入

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 (sp_product) LEFT OUTER JOIN sp_product_type ON sp_product_type .`tCat' at line 2

SELECT 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_type ON sp_product_type . tCategory = sp_product . type WHERE 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(); 

任何想法,我做错了?

回答

5

似乎问题不恰当的报价。

应该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(); 
+1

也许应该传递一个'false'参数到'select' - 因为错误的保护算法... – uzsolt 2012-02-17 10:09:46

+0

我仍然得到一个错误:你的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以在'sp_product'附近使用正确的语法)LEFT OUTER JOIN'sp_product_type' ON'sp_product_type'.''tCategory''at line 1 – Claremont 2012-02-17 20:18:14

0

看看在GROUP_CONCAT文档:

更改此:

GROUP_CONCAT(product_type.tName SEPARATOR 

要这样:

GROUP_CONCAT(product_type.tName SEPARATOR ', ') 

你混合'

“product.name ,product.price,product.perm_name,product.description,GROUP _CONCAT(product_type.tName SEPARATOR',')as product_type.tName');

试试这个:

"product.name, product.price, product.perm_name, product.description, GROUP_CONCAT(product_type.tName SEPARATOR ',') as product_type.tName"); 
+0

感谢您的帮助,但我仍然得到错误 – Claremont 2012-02-17 04:30:54

+0

所以,做这项工作? – 2012-02-17 06:01:41

+0

不,我无法得到这个工作 – Claremont 2012-02-18 22:11:17

1

你应该在你选择跳过转义使用虚假的第二个参数:

$this->db->select('GROUP_CONCAT(product_type.tName SEPARATOR ",") as product_type.tName', false); 
0

user319198是开了个好开始但他的回答不起作用。 工作答案如下。

$this->db->select('product.name, product.price, product.perm_name, product.description, GROUP_CONCAT(product_type.tName SEPARATOR) as 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(); 

通知我删除SEPARATOR ",",并更名为中(as product_type.tName')领域,是不是在数据库中的字段名。 这已经过测试并正在工作。

1

没有必要只在小组CONCAT提分离器作为组CONCAT逗号(,),因为默认情况下它会采取逗号(,)

0

对于#Claremont,#user319198,#FredTheLover,#Mosty Mostacho 在使用CodeIgniter开发任何项目来选择多个数据或在codeigniter的select查询中使用复合语句时,请记住,您需要定义select查询的第二个参数。像

$ this-> db-> select('(SELECT SUM(company.amount)FROM payments WHERE company.invoice_id = 4')AS amount_paid',FALSE);

因为 $ this-> db-> select()接受可选的第二个参数。如果将其设置为FALSE,则CodeIgniter不会尝试使用反引号来保护字段或表名。如果您需要复合选择语句,这很有用。

0

我想应该是这样的:

$this->db->select('product.name, product.price, product.perm_name, product.description') 
    ->select(' GROUP_CONCAT(product_type.tName SEPARATOR ",") as product_type.tName', FALSE); 
$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();