SqlServer基础查询 电脑版发表于:2020/9/6 22:27 select top 1 from table1 //--------返回表中第一条数据 select top 10 percent * from table1 //--------返回表中前10%条数据 select distinct name from table1 //--------按照name字段去重复后返回所有数据 select count(distinct name) from table1 //--------统计以name去重后的记录条数 select avg(qty),sum(qty),max(qty), min(qty) from table1 //--------统计所选字段求平均值,求和,取最大值,取最小值 条件查询 : select * from table1 where name= 'tn' //--------返回名称=tn的数据 select * from table1 where name like '%青峯%' ['tn%']['%tn'] //--------返回名称包含tn两字的数据 select * from table1 where name not like '%tn%' //--------返回名称不包含tn两字的数据 select * from table1 where name= 'tn' order by city desc[asc] //--------返回名称=tn的数据并按照城市排序 select count(*) from table1 group by name //--------返回按照名称分组后的数据 select count(*), name table1 group by name //--------返回按照名称分组后的各组统计条数 select count(qty), name table1 group by name //--------返回按照名称分组后的各组数量统计条数,这里旨在说明可以在分 组过程中使用聚合函数操作某个字段 select * from table1 where date between '2019-01-22' and '2019-10-11' //--------返回日期在2019-01-22和2019-12-31之间 的数据,包含2019-01-22和2019-10-11 select * from table1 where date > '2019-01-22' and date < '2019-12-31' //--------返回日期在2019-01-22和2019-12-31之间的数据,不包含2019-01-22和2019-12-31 select * from table1 where id in (1,2) //--------返回id=1和2的数据 select * from table1 where id=1 or id=2 //--------返回id=1或2的数据,这与上面例子效果一样,对于同一字段不同值的搜索,尽量用in 代替 or