2012-02-17 101 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();