2012-01-23 75 views
0

好吧,我想我錯過了一個組或什麼,基本上我需要添加,但不知道如何做我提供添加行或加入它們。MySQL查詢JOIN沒有真正加入

這裏是我的查詢

SELECT * FROM approved_business, business_stores, Real_Cash_Offers 
WHERE approved_business.id = business_stores.business_id 
    AND Real_Cash_Offers.storeid IN (business_stores.storeid) 
ORDER BY `approved_business`.`id` ASC 

這裏是輸出

1249 Jaggers Hair and Beauty 2012-01-22 19:11:05 1249 1 6139646071 112 Bridge Street Eltham 1 3095 Let Jagger Hair and Beauty set you up with the hot... 1372 1 50|5 2012-01-22 19:11:05 
1249 Jaggers Hair and Beauty 2012-01-22 19:11:05 1249 1 6139646071 112 Bridge Street Eltham 1 3095 Let Jagger Hair and Beauty set you up with the hot... 1372 1 100|10 2012-01-22 19:11:05 
1249 Jaggers Hair and Beauty 2012-01-22 19:11:05 1249 1 6139646071 112 Bridge Street Eltham 1 3095 Let Jagger Hair and Beauty set you up with the hot... 1372 1 250|30 2012-01-22 19:11:05 

我想是讓報價成爲一個數組或東西,所以只有一行。

NEW CODE

發現錯誤的JOIN SQL CODE

SELECT * FROM approved_business, business_stores, Real_Cash_Offers 
WHERE approved_business.id = business_stores.business_id 
    AND Real_Cash_Offers.business_id = approved_business.id 
    AND Real_Cash_Offers.storeid = business_stores.storeid 
ORDER BY `approved_business`.`id` DESC 

OUTPUT

id tradingname listed business_id storeid phone street suburb state postcode discription business_id storeid offer tstamp 
2582 Deeply Skin Medi Spa 2012-01-22 19:11:05 2582 1 0388224001 Suite 3 , 616 Park Rd Park Orchard 1 3114  2582 1 370|5 2012-01-22 19:11:05 
2582 Deeply Skin Medi Spa 2012-01-22 19:11:05 2582 1 0388224001 Suite 3 , 616 Park Rd Park Orchard 1 3114  2582 1 570|10 2012-01-22 19:11:05 
2582 Deeply Skin Medi Spa 2012-01-22 19:11:05 2582 1 0388224001 Suite 3 , 616 Park Rd Park Orchard 1 3114  2582 1 1570|15 2012-01-22 19:11:05 
+1

請出示你想要的輸出是什麼樣子的例子,而當前的SQL輸出的列名 –

回答

0

嘗試group_concat

SELECT id, group_concat(offer) FROM approved_business, business_stores, Real_Cash_Offers 
WHERE approved_business.id = business_stores.business_id 
    AND Real_Cash_Offers.storeid IN (business_stores.storeid) 
GROUP BY id 
ORDER BY `approved_business`.`id` ASC 
0

您應該使用GROUP_CONCAT()函數將結果合併到一行中。

例如:

SELECT GROUP_CONCAT(Real_Cash_Offers.offer) 
FROM ... 
0

您試圖串連整個查詢的結果行。這不是SQL(ANSI)標準的一部分,但您可以使用DBMS特定功能來完成此操作。對於MySQL,將是GROUP_CONCAT - 見Can I concatenate multiple MySQL rows into one field?

SELECT business_stores.business_id, business_stores.store_id, GROUP_CONCAT(Real_Cash_Offers.offer) as offers 
FROM approved_business 
INNER JOIN business_stores 
    ON approved_business.id = business_stores.business_id 
INNER JOIN Real_Cash_Offers 
    ON Real_Cash_Offers.storeid = business_stores.storeid 
GROUP BY business_stores.business_id, business_stores.store_id 
ORDER BY `approved_business`.`id` ASC