2

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

它可以工作,但它会显示所有条目(就像它应该显示的那样)但是是否可以只显示找到的项目总数,例如,每个国家/地区?例如它在中国显示 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

有人建议吗?

4

2 回答 2

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*/
于 2012-06-29T11:50:33.047 回答
0

我的 SQL 有点生疏了,但是…… GROUP BY 呢?

我发现的语法是:

SELECT column_name, aggregate_function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name

或者,在本页的末尾,他们用外行术语进行了解释。

我希望这有帮助!

于 2012-06-29T11:58:32.873 回答