我在MySql中有一个MonthlySubscriber表:
CREATE TABLE MonthlySubscriber (
MobileNo varchar(20) NOT NULL,
Month int(11) NOT NULL,
PRIMARY KEY (MobileNo, Month)
);
一个手机不能在一个多月内多次来。所以根据手机,我不得不加入我的桌子。
需求量的:
我想自己加入我的桌子,只想要第一张桌子的MobileNo和Month以及第二张桌子的月份。 我希望(所有mobileNo和Month的数量从第一个表和月份从第二个表)的顺序和组来自列(月份从第一个表和月份从第二个表)
我的MySql查询:
Select COUNT(t.MSI), t.Mon1 ,t.Mon2 from
( SELECT
MonthlySubscriber.MobileNo as MSI,
MonthlySubscriber.Month as Mon1,
MonthlySubscriber.Month as Mon2
FROM MonthlySubscriber as MonthlySubscriber
LEFT JOIN MonthlySubscriber as MonthlySubscriber on
(MonthlySubscriber.MobileNo = MonthlySubscriber.MobileNo )
ORDER by MonthlySubscriber.Month
)
t GROUP BY t.Mon1, t.Mon2;
我想在Hibernate条件查询中完全相同的查询。
实施例
表
> MobileNo || Month
> 852335421 || 1
> 852335421 || 2
> 852335421 || 5
> 994534554 || 3
> 924443443 || 4
> 994534554 || 7
> 864645353 || 1
result should come like this
>count_of_mobileNos || month 1 || month 2
> 2 || 1 || 1
> 1 || 1 || 2
> 1 || 1 || 5
> 1 || 2 || 2
> 1 || 2 || 5
> 1 || 3 || 3
> 1 || 3 || 7
> 1 || 4 || 4
> 1 || 5 || 5
> 1 || 7 || 7
注意月份是1-12的整数
答案 0 :(得分:0)
试试这个:
SELECT count(MobileNo),
count( case when m.Month=1 then m.Month else 0 end ) as Month 1,
count( case when m.Month=2 then m.Month else 0 end ) as Month 2,
count( case when m.Month=3 then m.Month else 0 end ) as Month 3,
.
.
.
count( case when m.Month=12 then m.Month else 0 end ) as Month 12
FROM MonthlySubscriber m
GROUP BY m.Month