Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > ADP及SQL SERVER

写了一个好复杂的SQL查询语句

时 间:2007-11-24 09:08:07
作 者:jimcloudy   ID:35  城市:福州
摘 要:这是在asp+sql2000中使用的查询语句,在其它地方可以参考着用。
主要掌握

1、SUM(CASE WHEN datediff(d, Sdate, getdate()) <= 365 THEN Snum ELSE 0 END) AS mystar
2、SUBSTRING(table1.house_name, 1, CHARINDEX('(', table1.house_name + '(') - 1) 
3、GROUP BY 、HAVING 
4、多表联合查询


正 文:

Select table1.id, table1.house_name,
      table1.house_type, table1.house_area,
      table1.house_bk, table1.house_address,
      table1.house_bh, table1.house_hx,
      table1.house_w, table1.house_mj,
      table1.house_zc, table1.house_dd,
      table1.lp_road, table1.house_floor,
      table1.house_jgtime, table1.house_zx,
      table1.house_cx, table1.house_zjjy,
      table1.house_username, table1.house_userid,
      table1.cs_list, table1.house_money,
      table1.house_adddate, table1.contact_man,
      table1.shenf, table1.contact_tel,
      table1.house_pass, table1.house_top,
      table1.house_bak01, loupanpic.pic_first, [user].username,
      [user].name, [user].sex, [user].sj, [user].myblogid,
      [user].myphoto, [user].myslogan, SUM(user_star.Snum) AS mynum,
      SUM(CASE WHEN datediff(d, Sdate, getdate()) <= 365 THEN Snum ELSE 0 END) AS mystar
FROM table1
      LEFT OUTER JOIN [user] ON table1.house_userid = [user].id
      LEFT OUTER JOIN loupanpic ON SUBSTRING(table1.house_name, 1, CHARINDEX('(', table1.house_name + '(') - 1)
      = loupanpic.loupanName AND loupanpic.lb = 1
      LEFT OUTER JOIN user_star ON [user].id = user_star.user_id AND [user].name = user_star.user_name
GROUP BY table1.id, table1.house_name,
      table1.house_type, table1.house_area,
      table1.house_bk, table1.house_address,
      table1.house_bh, table1.house_hx,
      table1.house_w, table1.house_mj,
      table1.house_zc, table1.house_dd,
      table1.lp_road, table1.house_floor,
      table1.house_jgtime, table1.house_zx,
      table1.house_cx, table1.house_zjjy,
      table1.house_username, table1.house_userid,
      table1.cs_list, table1.house_money,
      table1.house_adddate, table1.contact_man,
      table1.shenf, table1.contact_tel,
      table1.house_pass, table1.house_top,
      table1.house_bak01, loupanpic.pic_first, [user].username,
      [user].name, [user].sex, [user].sj, [user].myblogid,
      [user].myphoto, [user].myslogan
HAVING (table1.cs_list = 0) AND (table1.house_pass = 0) AND (table1.house_top = 1)
orDER BY table1.house_username

说明:

这是在asp+sql2000中使用的查询语句,在其它地方可以参考着用。
主要掌握

1、SUM(CASE WHEN datediff(d, Sdate, getdate()) <= 365 THEN Snum ELSE 0 END) AS mystar
2、SUBSTRING(table1.house_name, 1, CHARINDEX('(', table1.house_name + '(') - 1)
3、GROUP BY 、HAVING
4、多表联合查询

 



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助