根据国家/地区计算总结果

时间:2012-06-29 11:46:13

标签: mysql

我有一个查询,显示测试dbase中多个表的列。输出是来自世界各地的酒店及其相应的信息。我的搜索条件很简单;没有空白字段,我希望他们的电话号码不以+号开头。

它可以工作,但它显示所有条目(就像它应该)但是,是否可以只显示找到的项目总数,例如,每个国家/地区?例如,它在中国展示了5000条生产线,在意大利展示了3458条生产线,在加拿大展示了2050条生产线等等。是否可以只显示找到的总金额? 5000:中国 3458:意大利 2050年:加拿大 等等 等

我已经尝试过COUNT和HAVING但到目前为止它对我来说还没有用。我也在这里查了几个主题,但仍然没有成功。这是我的问题:

SELECT 
hotel.id HotelID, 
hotel.name Hotel_Name, 
hotel.tel_area Area_Number,
hotel.tel Phone_Number,
city.name City,
region.name Region,
country.name Country,
country.id CountryID,
country.continent Continent,
hotel.web Website

FROM mydata.hotel      as hotel
JOIN mydata.city       as city     ON hotel.city = city.id
JOIN mydata.region     as region   ON region.id = city.region
JOIN mydata.country    as country  ON country.id = region.country

where hotel.tel not like ''
and accos.tel not like '+%'

order by country.name

有人建议吗?

2 个答案:

答案 0 :(得分:2)

SELECT 
country.name Country,
COUNT(*)
FROM mydata.hotel      as hotel
JOIN mydata.city       as city     ON hotel.city = city.id
JOIN mydata.region     as region   ON region.id = city.region
JOIN mydata.country    as country  ON country.id = region.country

where hotel.tel not like ''
and accos.tel not like '+%'
GROUP BY Country
/*an ORDER BY is not really needed in this case, in MySQL GROUP BY contains an implicit ORDER BY*/

答案 1 :(得分:0)

我的SQL有点生疏,但是...... GROUP BY怎么样?

我找到的语法是:

SELECT column_name, aggregate_function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name

或者,在这个page的末尾,他们用外行术语来解释。

我希望这有帮助!