关于SQLSERVER中用SQL语句查询的一些个人理解

时间:2021-11-29 16:50:39

作为一个编程菜鸟说真的很难有什么见解,也就是写给一些刚学习编程的人,希望能给他们一些帮助吧!

SQLSERVER作为刚开始入门学习的数据库,SQL语句也并不算太难!说白了也就是建库,建表,建约束,对数据库表进行增删改查!

但是今天主要讲的是查询,最重要也是最难的一部分!

首先呢看这样一段代码:

select DNAME as 区县,SNAME as 街道,hos_type.HTID as 户型,PRICE as 价格,CONTENTS as 描述,COPY as 备注 from hos_house left outer
join sys_user on sys_user.UID=hos_house.UID
join hos_street on hos_street.SID=hos_house.SID
join hos_district on hos_district.DID=hos_street.SDID
join hos_type on hos_type.HTID=hos_house.HTID
and hos_house.UID in
(
 select UID from sys_user where UNAME='张三'
 )

这段代码用了左外连接查询,查询了张三所有的房屋信息!

select top 5 * from hos_house where
(
  HMID not in
  (
   select top 5 HMID from hos_house order by HMID
  )
)

这是一段简单的分页!

select HTNAME as 户型,UNAME as 姓名,DNAME as 区县,SNAME as 街道 from hos_house left outer
join sys_user on sys_user.UID=hos_house.UID
join hos_street on hos_street.SID=hos_house.SID
join hos_district on hos_district.DID=hos_street.SDID
join hos_type on hos_type.HTID=hos_house.HTID
where hos_district.DID in
(
 select SDID from hos_street where SDID in
  (
   select SID from hos_house group by SID having (COUNT(*)>=1)
  )group by SDID having (COUNT(*)>=2)
)order by SDID

这段代码比较难,查询了所在街道的,小区最少有一个小区,一个小区最少有两个租房信息,这段代码看懂不难,难得是自己写是否想的周全!