表单和mysql有困难。 3个表,1个表值的总和。表单提供了搜索的价值,但它不适用于“WHERE> ='$ search_total_rating”在错误的地方,我在这里做了一些非常错误。
$result = mysql_query("SELECT coffeeshops.*, services.*, ratings.*, sum(temp.total) as final_total FROM coffeeshops inner join services on coffeeshops.shop_id=services.shop_id
inner join ratings on coffeeshops.shop_id=ratings.shop_id
inner join (select SUM(comfort + service + ambience + friendliness + spacious)/(5) / COUNT(shop_id) AS total, shop_id FROM ratings GROUP BY shop_id) as temp on coffeeshops.shop_id=temp.shop_id WHERE >= '$search_total_rating'");
我不完全理解这一点,但我想要做的是总评级总和> =选择评级。我试图访问final_total,这不是我的数据库中的实际列,这就是为什么使用SUM来获得每个商店的总评级。希望这是代码的一个小小的改组。感谢
答案 0 :(得分:1)
您应该使用 而不是
SELECT coffeeshops.*, services.*, ratings.*, sum(temp.total) as final_total
FROM coffeeshops inner join services on coffeeshops.shop_id=services.shop_id
inner join ratings on coffeeshops.shop_id=ratings.shop_id
inner join (
select SUM(comfort + service + ambience + friendliness + spacious)/5/ COUNT(shop_id) AS total, shop_id
FROM ratings GROUP BY shop_id)
as temp on coffeeshops.shop_id=temp.shop_id
having final_total >= '$search_total_rating'
答案 1 :(得分:0)
您已在子查询中计算了总计。无需第二个SUM()
。
SELECT coffeeshops.*
, services.*
, ratings.*
, temp.total as final_total
FROM coffeeshops
inner join services
on coffeeshops.shop_id = services.shop_id
inner join ratings
on coffeeshops.shop_id = ratings.shop_id
inner join
( select SUM(comfort + service + ambience + friendliness + spacious) / 5
/ COUNT(shop_id) AS total
, shop_id
FROM ratings
GROUP BY shop_id
) as temp
on coffeeshops.shop_id = temp.shop_id
WHERE temp.total >= '$search_total_rating'
您还可以在子查询中使用HAVING
:
SELECT coffeeshops.*
, services.*
, ratings.*
, temp.total as final_total
FROM coffeeshops
inner join services
on coffeeshops.shop_id = services.shop_id
inner join ratings
on coffeeshops.shop_id = ratings.shop_id
inner join
( select SUM(comfort + service + ambience + friendliness + spacious) / 5
/ COUNT(shop_id) AS total
, shop_id
FROM ratings
GROUP BY shop_id
HAVINGE total >= '$search_total_rating'
) as temp
on coffeeshops.shop_id = temp.shop_id