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、多表联合查询