Mysql查询-连接和WHERE子句,初学者:)
在表单和mysql方面有困难。3个表,1个表值的总和。表单提供了要搜索的值,但它不适用于“WHERE>=”$search\u total\u rating”的位置错误,我在这里做了一些非常错误的事情Mysql查询-连接和WHERE子句,初学者:),mysql,Mysql,在表单和mysql方面有困难。3个表,1个表值的总和。表单提供了要搜索的值,但它不适用于“WHERE>=”$search\u total\u rating”的位置错误,我在这里做了一些非常错误的事情 $result = mysql_query("SELECT coffeeshops.*, services.*, ratings.*, sum(temp.total) as final_total FROM coffeeshops inner join servic
$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来获得每个商店的总评级。希望这只是对代码的一个小改动。谢谢您应该使用have而不是where
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'
您已经在子查询中计算了总计。不需要第二次
SUM()
您还可以在子查询中使用
have
:
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
部分但不是全部,下面的帖子修复了它,我想:)修复:)非常感谢,所以它使用了2个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
HAVINGE total >= '$search_total_rating'
) as temp
on coffeeshops.shop_id = temp.shop_id