有两张表
第一张表为归属地库表mobilephone
字段:
第二张表为来电记录表mobileinterface
当有来电时候会记录来电的信息,以及归属地(area)。
需求:
查询出当前所有来电的排名,哪个区域最多。要求显示省,市,区号,数量。
只查mobileinterface为下图
------解决方案--------------------
SELECT b.province,b.city,a.*
FROM
(
SELECT TOP 10 area,COUNT(*) AS line
FROM mobileinterface ORDER BY 2 desc
) a
JOIN mobilephone b ON a.area=b.area
ORDER BY a.line desc
------解决方案--------------------
select
top 10 a.*,b.province,b.city
from
(select area,count(1) as line from mobileinterface group by area) as a
inner join
mobilephone as b ON a.area=b.area
order by
a.line desc
------解决方案--------------------
改成这样呢:
SELECT b.province,b.city,a.*
FROM
(
SELECT TOP 10 area,COUNT(*)
AS line FROM mobileinterface
group by area
ORDER BY line desc
) a
JOIN mobilephone b ON a.area=b.area
ORDER BY a.line desc