MySQL查询JOIN并没有真正加入

时间:2012-01-23 21:45:24

标签: mysql sql join inner-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

我想要的是让优惠成为一个数组或其他东西,所以只有一行。

新代码

在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

输出

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您应该使用GROUP_CONCAT()功能将结果合并为一行。

例如:

SELECT GROUP_CONCAT(Real_Cash_Offers.offer)
FROM ...

答案 2 :(得分:0)

您正在尝试连接查询的结果行。这不是SQL(ANSI)标准的一部分,但您可以使用DBMS特定的功能来实现此目的。对于将为GROUP_CONCAT的MySQL - 请参阅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