douejuan9162 2014-06-04 21:49
浏览 31
已采纳

mysql select * with count

I have this query

SELECT *, COUNT(*) as count FROM PricePaid WHERE Postcode LIKE 'L23 0TP%' GROUP BY Postcode

I am getting this result :

L23 0PT   House number 1    Bella Grove     LIVERPOOL   2 

But what I am looking for is a way to get this result :

L23 0PT   House number 1    Bella Grove     LIVERPOOL   2
L23 0PT   House number 17   Bella Grove     LIVERPOOL   2  

You see I am displaying the results on a google map So initially I want to display a marker on the map showing each post code with the number of houses sold on the post code ( the count(*)part )

And then I need display a list of the propeties from that street in the same query to pass to an info window to then display all the properties on that post code.

Any one any idea if I can do this on one query, any pointers would be greatly appreciated thanks

  • 写回答

2条回答 默认 最新

  • doubi2145 2014-06-04 23:46
    关注

    You are misusing a nonstandard MySQL extension to GROUP BY. See this: http://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html

    You have two things going on in this query. One is to provide some details from your table. The other is to present an aggregate. You can't do both without using a subquery.

    Here is what you need:

    SELECT pp.*, cpp.count
      FROM PricePaid AS pp
      JOIN (
             SELECT Postcode, COUNT(*) AS count
               FROM PricePaid 
              GROUP BY PostCode
           ) AS cpp ON pp.Postcode = cpp.Postcode
     WHERE pp.Postcode LIKE 'L23 0TP%' 
    

    Do you see the virtual summary (aggregate) table JOINed to your physical table? It does the count of properties by postcode.

             SELECT Postcode, COUNT(*) AS count
               FROM PricePaid 
              GROUP BY PostCode
    

    The JOIN picks up the count for all the properties in the Postcode and includes it in the result set rows with the detail items.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器