|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
这章描述如何检查和处理在MySQL数据库中的数据损坏。如果你的表损坏很多,你应该尝试找出其原因!见G.1调试一个MySQL服务器。语句
1.用一条语句得出某日期地点月份的最年夜天数?
SELECTDAY(DATEADD(dd,-DAY(2004-02-13),DATEADD(mm,1,2004-02-13)))ASDayNumber
2.少纪录酿成多笔记录成绩
有表tbl
日期支出付出
2004-02-1100:00:006045
2004-03-0100:00:006045
2004-03-0200:00:004050
2004-03-0500:00:005040
/*
测试数据:
CreateTabletbl([日期]smalldatetime,[支出]int,[付出]int)
InsertIntotbl
SELECT2004-02-11,60,45
unionSELECT2004-03-01,60,45
unionSELECT2004-03-02,40,50
unionSELECT2004-03-05,50,40
*/
要失掉的了局:
日期支出付出余额
2004-02-0100:00:00NULLNULLNULL
2004-02-0200:00:00NULLNULLNULL
2004-02-0300:00:00NULLNULLNULL
2004-02-0400:00:00NULLNULLNULL
2004-02-0500:00:00NULLNULLNULL
2004-02-0600:00:00NULLNULLNULL
2004-02-0700:00:00NULLNULLNULL
2004-02-0800:00:00NULLNULLNULL
2004-02-0900:00:00NULLNULLNULL
2004-02-1000:00:00NULLNULLNULL
2004-02-1100:00:00604515
2004-02-1200:00:00NULLNULL15
2004-02-1300:00:00NULLNULL15
2004-02-1400:00:00NULLNULL15
2004-02-1500:00:00NULLNULL15
2004-02-1600:00:00NULLNULL15
2004-02-1700:00:00NULLNULL15
2004-02-1800:00:00NULLNULL15
2004-02-1900:00:00NULLNULL15
2004-02-2000:00:00NULLNULL15
2004-02-2100:00:00NULLNULL15
2004-02-2200:00:00NULLNULL15
2004-02-2300:00:00NULLNULL15
2004-02-2400:00:00NULLNULL15
2004-02-2500:00:00NULLNULL15
2004-02-2600:00:00NULLNULL15
2004-02-2700:00:00NULLNULL15
2004-02-2800:00:00NULLNULL15
2004-02-2900:00:00NULLNULL15
2004-03-0100:00:00604530
2004-03-0200:00:00405020
2004-03-0300:00:00NULLNULL20
2004-03-0400:00:00NULLNULL20
2004-03-0500:00:00504030
2004-03-0600:00:00NULLNULL30
2004-03-0700:00:00NULLNULL30
2004-03-0800:00:00NULLNULL30
2004-03-0900:00:00NULLNULL30
2004-03-1000:00:00NULLNULL30
2004-03-1100:00:00NULLNULL30
2004-03-1200:00:00NULLNULL30
2004-03-1300:00:00NULLNULL30
2004-03-1400:00:00NULLNULL30
2004-03-1500:00:00NULLNULL30
2004-03-1600:00:00NULLNULL30
2004-03-1700:00:00NULLNULL30
2004-03-1800:00:00NULLNULL30
2004-03-1900:00:00NULLNULL30
2004-03-2000:00:00NULLNULL30
2004-03-2100:00:00NULLNULL30
2004-03-2200:00:00NULLNULL30
2004-03-2300:00:00NULLNULL30
2004-03-2400:00:00NULLNULL30
2004-03-2500:00:00NULLNULL30
2004-03-2600:00:00NULLNULL30
2004-03-2700:00:00NULLNULL30
2004-03-2800:00:00NULLNULL30
2004-03-2900:00:00NULLNULL30
2004-03-3000:00:00NULLNULL30
2004-03-3100:00:00NULLNULL30
谜底:
SELECTY.[日期],tbl.[支出],tbl.[付出],(
SELECTSUM(ISNULL(tbl.[支出],0)-ISNULL(tbl.[付出],0))FROMtblWHERE[日期]<=Y.[日期])AS[余额]
FROMtblRIGHTJOIN(
SELECTDATEADD(dd,N.i,DATEADD(dd,1-DAY(m.MinDay),m.MinDay))AS[日期]
FROM(
SELECT0ASi
UNIONALLSELECT1
UNIONALLSELECT2
UNIONALLSELECT3
UNIONALLSELECT4
UNIONALLSELECT5
UNIONALLSELECT6
UNIONALLSELECT7
UNIONALLSELECT8
UNIONALLSELECT9
UNIONALLSELECT10
UNIONALLSELECT11
UNIONALLSELECT12
UNIONALLSELECT13
UNIONALLSELECT14
UNIONALLSELECT15
UNIONALLSELECT16
UNIONALLSELECT17
UNIONALLSELECT18
UNIONALLSELECT19
UNIONALLSELECT20
UNIONALLSELECT21
UNIONALLSELECT22
UNIONALLSELECT23
UNIONALLSELECT24
UNIONALLSELECT25
UNIONALLSELECT26
UNIONALLSELECT27
UNIONALLSELECT28
UNIONALLSELECT29
UNIONALLSELECT30
UNIONALLSELECT31
)N,
(
SELECTMIN(日期)ASMinDay
FROMtbl
GROUPBYDATEDIFF(month,0,日期)
)M
WHEREDATEDIFF(mm,DATEADD(dd,N.i,DATEADD(dd,1-DAY(m.MinDay),m.MinDay)),M.MinDay)=0)ASY
ONtbl.[日期]=Y.日期
MyISAMMysql的默认数据库,最为常用。拥有较高的插入,查询速度,但不支持事务 |
|