SQL server常用操作

查询:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count(id) as totalcount from table1
求和:select sum(id) as sumvalue from table1
平均:select avg(id) as avgvalue from table1
最大:select max(id) as maxvalue from table1
最小:select min(id) as minvalue from table1

链接查询

1、inner join 内连接查询

    select * from student a inner join course b on a.id=b.id

2、left (outer) join: 左链接查询
    左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。 
    SQL: select * from student a left join course b on a.id=b.id
3:right (outer) join: 右链接查询

    外连接(连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
    SQL: select * from student a right join course b on a.id=b.id

4:full/cross (outer) join完全链接查询
    全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

    SQL: select * from student a full  join course b on a.id=b.id

子查询和内嵌查询

1、子查询

    select * from student where id in (select id from course)

  或   select * from student where id in (1,2,3)

2、带 in 的嵌套查询  not in与in查询相反

  select * from student where id in (1,2,3) 查询id为1,2,3的数值

3、some 和any 和 all 查询

    select * from student where idd >(select id from course ) 如果不加any 数据库是不执行的 正确的select * from student where idd >any(select id from course )

    some 和any 是等效的,all要求where的查询表达式与子查询返回的每个值进行比较时都应满足条件,some和any则要求where的表达式与子查询返回的值进行比较时至少有一个满足条件

    some : select * from student where idd >some(select id from course )

    any :    select * from student where idd >any(select id from course )

     all :      select * from student where idd >any(select id from course )

4、使用case函数进行查询   

    select *,
    case
    when idd>=5 then '优秀'
    when idd>=10 and idd<=6 then '中等'
    else '不及格'
    end
    from student

    

 模糊查询与范围查询

 1 、between  and 查询

    select * from student where idd between 3 and 4 查询3和4之间的数据,包括3和4。

2、% 查询

    select * from student where name like '%程%'     查询名字中包含‘程’ 的数值

3、_ 查询

    select * from student where name like '程_'     查询名字中开头包含‘程’ 的数值
    select * from student where name like '_g'  查询名字中结尾包含‘g’ 的数值

4、[] 查询 

    select * from student where name like '[mr]%' 查询以mr开头的数值

    select * from student where name like '[^mr]%' 查询不是以mr开头的数值

转载于:https://www.cnblogs.com/qhantime/p/10705185.html

SQL server常用查询相关推荐

  1. SQL Server 常用查询练习

    生成测试数据 if exists(select * from sysobjects where name='db_user') drop table db_user gocreate table db ...

  2. SQL Server 语句查询手册

    建表: CREATE TABLE  [DB.dbo].tableName (Stud_id int CONSTRAINT  constraintName1  not null primary key, ...

  3. [转]Sql Server参数化查询之where in和like实现详解

    本文转自;http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARIND ...

  4. SQL Server 2016 查询存储性能优化小结

    SQL Server 2016已经发布了有半年多,相信还有很多小伙伴还没有开始使用,今天我们来谈谈SQL Server 2016 查询存储性能优化,希望大家能够喜欢 作为一个DBA,排除SQL Ser ...

  5. SQL Server数据库查询速度慢的原因和解决方法

    SQL Server数据库查询速度慢的原因和解决方法 参考文章: (1)SQL Server数据库查询速度慢的原因和解决方法 (2)https://www.cnblogs.com/MyChange/p ...

  6. 优化SQL Server数据库查询方法

    本文详细介绍了优化SQL Server数据库查询方法. SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) ...

  7. SQL Server 2000查询n到m条记录

    SQL Server 2000查询n到m条记录? (1)select top m * from tablename where id not in (select top n id from tabl ...

  8. SQL Server 2005查询处理结构-用户模式计划(UMS)

    SQL Server 2005查询处理结构-用户模式计划(UMS) 在对数据库进行性能调优时,必须全面的考虑各种可能造成系统性能瓶颈的各种因素,因此深入了解SQL Server 2005的查询处理机构 ...

  9. SQL Server数据库查询区分大小写、全半角——排序规则的应用(转载)

    SQL Server数据库查询区分大小写.全半角--排序规则的应用 因为偶然的原因,需要在INNER JOIN联表时,让对应字段进行区分大小写的比较.而默认情况下建立的Sql Server数据库是不区 ...

最新文章

  1. 比特币现金是银行的一种补充
  2. 《MongoDB管理与开发精要》——1.4节本章小结
  3. 关闭oracle自动统计,禁用oracle 11g 的统计数据自动功能
  4. 树莓派Raspberry Pi OS开机自启动脚本
  5. 2017.9.27 青蛙的约会 失败总结
  6. hive之分区表详解
  7. LNAMP服务器环境(源码安装)
  8. oracle巡检计划书,网络巡检计划表.doc
  9. matlab实现聚类分析的优点,MATLAB感悟(3)--聚类分析
  10. Qt疑难杂症之编译QPA插件
  11. Delta并联机构重力补偿分析
  12. 如何用GoldWave中文版提取伴奏?
  13. [微软中国]秋季校园招聘开启了(可内推)
  14. Java百度网盘上传图片(文件)
  15. android 异源投屏,华为MATE10支持异源投屏是什么意思?
  16. 微信引流最有效的方法
  17. 如何避开PPT演讲的几个误区(中)
  18. java kml_当Java遇上KML
  19. 门禁控制器的选购指南
  20. 什么是你的核心竞争力之六善用你的弱点

热门文章

  1. object类型转换为Array类型
  2. 织梦直接往数据库写入数据
  3. Opencl 并行求和
  4. struts2线程安全
  5. MVC的传递数据的方法
  6. sharepoint被阻止的文件类型解释说明
  7. 文件名(后缀名)修改
  8. mybatis foreach collection
  9. Xshell6突然连不上K8S所在的虚拟机
  10. 树莓派Java程序运行_树莓派上Java程序作为linux服务并开机自动启动