我试图获取每个城市中没有旅馆的数量。
我有2张桌子。第一张桌子是hotel_master
lval *z = malloc(sizeof(lval));
z->value.sexpr1 = malloc(sizeof(sexpr));
z->value.sexpr1->cell= NULL;
第二个表是city_master
hotel_master
id | hotel_name | city_code
是否可以从查询中获取计数?
首先,我已经尝试过此查询,或者我需要使用do while循环?
city_master
id | city_code | city_name
请帮助我。
答案 0 :(得分:3)
SELECT COUNT(h.city_code) as total_hotel, c.city_name
FROM hotel_master h
INNER JOIN city_master c ON h.city_code = c.city_code
GROUP BY h.city_code
在COUNT
中的字段上使用hotel_master
。它将根据城市获得旅馆的数量。
答案 1 :(得分:2)
您可以尝试以下方法:
SELECT COUNT(*)
FROM hotel_master
JOIN city_master
ON hotel_master.city_code = city_master.city_code
GROUP
BY hotel_master.city_code
答案 2 :(得分:2)
只需使用COUNT()
,GROUP BY
就可以做到
SELECT
count(hotel_master.hotel_name),
city_master.city_name
FROM hotel_master
INNER JOIN city_master ON hotel_master.city_code = city_master.city_code
GROUP BY city_master.city_name
答案 3 :(得分:2)
需要在查询中使用GROUP BY
子句来获取每个城市的旅馆数量。
SELECT city_master.city_name, count(hotel_master.id) hotetcount
FROM hotel_master
JOIN city_master
ON hotel_master.city_code = city_master.city_code
GROUP
BY city_master.city_code