这是我的表,我需要获得如下结果:
in 560038 two hospitals, one temple, zero masjid, zero company, one church and
in 560049 one schools, one masjid, one company, zero temple, zero hospitals, and
in 562129 one high school, zero hospitals, zero company, zero masjid , zero temple.
604=hospital,
1306=temple,
1302=church,
204=school,
1305=masjid,
102=company.
是否可以从mysql中的单个查询中获取?在这里,我只能给出city_id。
id |cat_id|poi_id |addr |pincode|city_id
1 |6 |604 |Amrutha Hospital |560038 |284
2 |13 |1306 |Temple |560038 |284
3 |13 |1302 |Resurrection Church |560038 |284
4 |2 |204 |Lake Montfort School |560049 |284
5 |6 |604 |Garden City Hospital |560038 |284
6 |13 |1305 |Bilal Masjid |560049 |284
7 |1 |102 |Grindwell Limited |560049 |284
8 |2 |204 |Citizen High School |562129 |284
答案 0 :(得分:1)
您可以在COUNT()
中使用CASE
表达式有条件地汇总poi_id
以区分计数,按pincode
分组。
你想:
SELECT pincode,
COUNT(CASE poi_id WHEN 604 THEN 1 END) AS hospital_cnt,
COUNT(CASE poi_id WHEN 1306 THEN 1 END) AS temple_cnt,
COUNT(CASE poi_id WHEN 1302 THEN 1 END) AS church_cnt,
COUNT(CASE poi_id WHEN 204 THEN 1 END) AS school_cnt,
COUNT(CASE poi_id WHEN 1305 THEN 1 END) AS masjid_cnt,
COUNT(CASE poi_id WHEN 102 THEN 1 END) AS company_cnt
FROM tbl
GROUP BY pincode
修改:根据您的评论,您可以获得每poi_id
个pincode
每个可能的计数的行格式:
SELECT a.pincode,
b.poi_id,
COUNT(c.pincode) AS cnt
FROM (SELECT DISTINCT pincode FROM tbl) a
CROSS JOIN poi b
LEFT JOIN tbl c ON a.pincode = c.pincode AND b.poi_id = c.poi_id
GROUP BY a.pincode, b.poi_id
poi
是poi_id
链接到的表的名称。确保列名称也一致。
如果您仍需要每pincode
行一行,则可以使用GROUP_CONCAT
将所有计数带入一个分隔的字符串中:
SELECT a.pincode,
GROUP_CONCAT(CONCAT(a.poi_id, ': ', a.cnt) SEPARATOR ' / ') AS poi_cnts
FROM (
SELECT a.pincode,
b.poi_id,
COUNT(c.pincode) AS cnt
FROM (SELECT DISTINCT pincode FROM tbl) a
CROSS JOIN poi b
LEFT JOIN tbl c ON a.pincode = c.pincode AND b.poi_id = c.poi_id
GROUP BY a.pincode, b.poi_id
) a
GROUP BY a.pincode
答案 1 :(得分:0)
我认为这就是你要找的东西:
SELECT pincode, poi_id, COUNT(1) AS place_cnt
FROM table_name
GROUP BY pincode, poi_id;
答案 2 :(得分:0)
select pincode, addr, count(id) from YourTable group by pincode, addr
答案 3 :(得分:-1)
如果我理解正确,你需要一个结果集,其中每一行都是pincode,后跟每种兴趣点的数量。
您指定的目标结果看起来像是一个长而自然的英文字符串。我不会让你那么远,但我会给你一些你可以在代码中使用的东西。
以下是最终结果:
pincode | hospitals | temples | masjids | companies | churches | schools
560038 | 2 | 1 | 0 | 0 | 1 | 0
560049 | 0 | 0 | 1 | 1 | 0 | 1
562129 | 0 | 0 | 0 | 0 | 0 | 1
注意:我不认为以这种方式要求结果是理想的。如果稍后添加新类型的兴趣点,则必须重写查询。但由于这是一个问题,这就是答案:
有两种方法可以做到这一点,并且两种方法都使用对每种类型的兴趣点计数(*)的子查询。第一个选项是将子查询嵌入查询的SELECT部分。第二个选项是从子查询中创建派生表并将它们连接在一起。
这是选项1:
SELECT
mainTbl.pincode,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 604) as hospitals,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1306) as temples,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1305) as masjids,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1302) as churches,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 204) as schools,
(SELECT count(*) FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 102) as companies
FROM myTable mainTbl
WHERE mainTbl.city_id = ?
选项2:
SELECT
mainTbl.pincode,
hospitalsTbl.hospitals,
templesTbl.temples,
masjidsTbl.masjids,
churchesTbl.churches,
schoolsTbl.schools,
companiesTbl.companies
FROM myTable mainTbl
JOIN (SELECT count(*) as hospitals, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 604) hospitalsTbl ON hospitalsTbl.pincode = mainTbl.pincode
JOIN (SELECT count(*) as temples, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1306) templesTbl ON templesTbl.pincode = mainTbl.pincode
JOIN (SELECT count(*) as masjids, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1305) masjidsTbl ON masjidsTbl.pincode = mainTbl.pincode
JOIN (SELECT count(*) as churches, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 1302) churchesTbl ON churchesTbl.pincode = mainTbl.pincode
JOIN (SELECT count(*) as schools, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 204) schoolsTbl ON schoolsTbl.pincode = mainTbl.pincode
JOIN (SELECT count(*) as companies, pincode FROM myTable WHERE pincode = mainTbl.pincode AND city_id = mainTbl.city_id AND poi_id = 102) companiesTbl ON companiesTbl.pincode = mainTbl.pincode
WHERE mainTbl.city_id = ?
正如您所看到的,查询非常详细。有这样的事情要好得多:
SELECT count(*) as num_poi, poi_id, pincode
FROM myTable
WHERE city_id = ?
GROUP BY poi_id, pincode
然后在代码中构建一行代码。