1

这是我的表,我需要得到如下结果:

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 
4

4 回答 4

1

您可以使用其中的CASE表达式COUNT()有条件地聚合poi_ids 以区分计数,按 分组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
于 2012-08-03T10:03:40.947 回答
0
select pincode, addr, count(id) from YourTable group by pincode, addr
于 2012-08-03T09:34:13.330 回答
0

我认为这就是你要找的:

SELECT pincode, poi_id, COUNT(1) AS place_cnt
FROM table_name
GROUP BY pincode, poi_id;
于 2012-08-03T09:32:27.307 回答
-1

如果我理解正确,您需要一个结果集,其中每一行都是密码,后跟每种兴趣点的数字。

您指定的目标结果看起来需要是一个很长的自然英文字符串。我不会让你走那么远,但我会给你一些你可以在代码中使用的东西。

这是您最终会得到的最终结果:

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

注意:我认为以这种方式要求结果并不理想。如果稍后添加新类型的兴趣点,您将不得不重写查询。但既然是这个问题,那就是答案:

有两种方法可以做到这一点,并且都使用对每种类型的兴趣点执行 count(*) 的子查询。第一个选项是将子查询嵌入到查询的 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

然后在代码中构建你的一行行。

于 2012-08-03T10:15:55.070 回答