日期:2014-05-18  浏览次数:20544 次

求解一个SQL语句
SQL语句不熟,麻烦哪位大侠解释下下面这句是什么意思?tmpDangerAllAllSql 是用来统计所有危险点的



tmpDangerAllAllSql = "SELECT COUNT(*) as dangerAll FROM G_DANGER where (convert(char(4),find_date,120)<=getdate()) and DANGER_NO <> 1 and DANGER_LEVEL <> (select subid from g_datadict where dictid=3 and dictvalue='白色')";

------解决方案--------------------
SQL code

SELECT COUNT(*) as dangerAll FROM G_DANGER --查询G_DANGER表
where (convert(char(4),find_date,120)<=getdate())  --年度比当年时间早
and DANGER_NO <> 1    --DANGER_NO值不等于1
and DANGER_LEVEL <> (select subid from g_datadict where dictid=3 and dictvalue='白色') --子查询做条件

------解决方案--------------------
convert(char(4),find_date,120) CONVERT 是格式转化函数 CHAR(4)是转化成长度为4的字符串,120是转化的时间格式(120表示2006-05-16 10:57:49这种形式的时间格式),总的来说就是把find_date转化为120所示格式的日期,然后结果放到长度为4的字符串里面,也就是左面截取4位,恰好就是日期的年份。