写了一个好复杂的SQL查询语句-许积云
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> ADP及SQL SERVER


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

发表时间:2007/11/24 9:08:07 评论(1) 浏览(8683)  评论 | 加入收藏 | 复制
   
摘 要:这是在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群(群号:198465573)
 
 相关文章
[access报表]access如何制作复杂报表  【umvsoft整理  2008/7/23】
进度条不必太复杂  【马喜立  2011/11/25】
异曲同工——使用代码解决复杂查询,查询日期补空行  【在水一方  2013/4/1】
分步法实现复杂查询(基本不用SQL语句)  【半夜罗  2014/6/24】
常见问答
技术分类
相关资源
文章搜索
关于作者

许积云

文章分类

文章存档

友情链接