MSSQL DBOtherSQL
--------------------------查询表中的数据------------------------------ --1.请查询出MyStudent表中的所有数据
--下面的语句表示查询出MyStudent表中的所有行、所有列
select * from MyStudent --2.只显示部分列的数据(现实所有行,但是只查看指定的列)
select fname,fage,fgender from mystudent --3.请查询出年龄在20-24岁之间的所有女同学的姓名,年龄,性别,学号
select fname,fage,fgender,fid from MyStudent
where fgender='女' and fage>=20 and fage<=24 --4.为列起别名
select
fname as 姓名,
fage as 年龄,
fgender as 性别,
fid as 学号
from MyStudent
where fgender='女' and fage>=20 and fage<=24 select
fname 姓名,
fage 年龄,
fgender 性别,
fid 学号
from MyStudent
where fgender='女' and fage>=20 and fage<=24 select
'姓 名'=fname, --对于那些非法的命名方式,可以使用[]或者''包含起来。
年龄=fage,
性别=fgender,
学号=fid
from MyStudent
where fgender='女' and fage>=20 and fage<=24 --------Select语句可以单独使用
select A='你好',B='世界' select getdate() sp_helptext 'sp_rename' select * from TblTeacher --对已经通过select语句查询出的结果集,进行去除重复操作distinct
select
distinct
ttname,
ttgender,
ttage
from tblteacher select
distinct
ttid,
ttname,
ttgender,
ttage
from tblteacher select distinct ttname from tblteacher
-----------------------------------------------------
---top,获取查询出的结果集中的前n条。
--order by 进行排序。所以,一般使用top的时候,必须配合排序一起使用才有意义。
select * from MyStudent --查找年龄最小的前5名同学
--asc 表示升序排序,从小到大。
--desc表示降序排序,从大到小。
--默认不写,表示是asc,升序。
select top 5 * from MyStudent order by fage asc,fmath desc
select top (5*3) * from MyStudent order by fage asc,fmath desc --------\
select top 5 percent * from MyStudent order by fage asc,fmath desc ------------聚合函数---------------------
--求总和的聚合函数
select * from TblScore select
数学成绩总分=sum(tMath)
from TblScore ---求最大值
select
数学成绩总分=max(tMath)
from TblScore
--求最小值
select
数学成绩总分=min(tMath)
from TblScore
--求平均值
--注意:求平均值得时候如果表中的列的数据类型是整数,那么最后计算出的平均值可能有问题
--因为 整数/整数 最后得到的结果还是整数。解决:把其中一个数据转换为小数。 值*1.0
select
数学成绩总分=avg(tMath)
from TblScore ---请查询出,所有那些tmath的值大于90分的人的个数。
--count()聚合函数,最后返回的是所查询出的记录的条数。
--如果没有查询出任何数据,那么count()聚合函数返回值是0
select count(*) from TblScore where tmath>750 select * from TblScore --聚合函数不统计null值。
select count(tEnglish) from TblScore select count(*) from TblScore
select count(1) from TblScore select
数学成绩总分=sum(tEnglish)
from TblScore --注意:avg()聚合函数同样不统计null值。
select
数学成绩总分=avg(tEnglish)
from TblScore select * from MyStudent
select
max(FBirthday) as maximum,
min(FBirthday) as minimum
from MyStudent
where fgender='男' select * from TblScore;
select
tsid,
tenglish,
tmath,
个人平均分=(tEnglish+tMath)/2
from TblScore
order by 个人平均分 desc select * from TblTeacher select count(ttname) from TblTeacher select COUNTDISTINCT(ttname) from TblTeacher --查询没有及格的学生的学号
select * from TblScore;
select tsid from TblScore where tmath<60 --查询年龄在20-30岁之间的男学生
select * from TblStudent where tsage>=20 and tsage<=30 and tsgender='男'
select * from TblStudent where tsage between 20 and 30 and tsgender='男' --Between…and … 在...之间,(闭区间,包含两个端点值) --查询年龄在20-30岁之间的男学生 --查询math成绩在80-90分之间的所有学生
select * from TblScore;
select * from TblScore where tmath between 80 and 90 -----------------------------------------------------------------
select * from TblStudent select * from TblClass select * from TblStudent where tsclassid=5 or tsclassid=3 or tsclassid=1 select * from TblStudent where tsclassid in (5,3,1) --其实这里使用in()等价于使用or select * from TblStudent where tsclassid in (3,4,5) --如果使用in的时候,小括号中的值恰好是一个连续的值,那么建议改成>= and <=的方式,这样可以很好地使用索引。可以提高性能。
select * from TblStudent where tsclassid>=3 and tsclassid<=5 --------------------------模糊查询---------------------------------------
select * from TblStudent -- % 表示可以匹配任意多个任意字符。
--查询所有以"张"字开头的人
select * from TblStudent where tsname like '张%' --查询那些所有以"张"字开头的,并且是两个字的人。
select * from TblStudent where tsname like '张%' and len(tsname)=2 --ctrl + R -- _ 表示可以匹配任意的单个字符。_ 必须匹配一个字符,没有字符不行 select * from TblStudent where tsname like '张_' -- [] 这里的含义与正则表达式中的含义是一致的,都是表示在中括号的范围内任意匹配一个字符。 select * from TblStudent where tsname like '_[a-z0-9]_' -- ^ 在中括号中的时候,表示非的意思。 select * from TblStudent where tsname like '_[^a-z0-9]_' --当希望通配符只表示一个普通字符的时候,此时应该将通配符放到[]中。
select * from TblStudent where tsname like '%[_]%' --[[] select * from TblStudent where tsname like '张%' select * from TblStudent where tsname not like '张%' select * from TblStudent where tsname like '[^张]%'
---------------------------------------对于数据库中的null值处理--------------------------------
--数据库中的null值比较特殊,不能用=或者<>来进行比较。
select * from TblStudent where tsage=null select * from TblStudent where tsage<>null ------------------------------------------------
--数据库中的null值,比较的时候必须使用is null或者是is not null来进行判断,绝对不能直接使用=或者<>
select * from TblStudent where tsage is null
--isnull() select * from TblStudent where tsage is not null select tsage+10 from TblStudent where tsid=1 --null值与任何其他值计算后结果还是null值。
select tsage+10 from TblStudent where tsid=2 ----------------------------- 排序 order by----------------- --1>使用方式:
--order by 列1 asc,列2 desc
--2>查询语句的基本结构 --select
-- 选择列 -------------【三】
--from 表 -------------【一】
--where 条件(其实就是对表中数据的筛选,主要对数据行的筛选) -------------【二】
--order by 列 (对数据先通过where条件进行筛选,然后对筛选后的数据再进行排序)-------------【四】
--无论一条查询语句有多么复杂,一般情况下,order by 语句永远都是最后执行。
select * from TblScore --当一个查询使用了order by 以后,那么这个查询,查询出的结果就是一个有序的结果,
--既然是一个有序的结果,就不是一个集合了(集合是无序的),所以如果一个查询希望被另外一个查询使用,那么不要使用order by(除非还同时使用了top)
select
tscoreId,
tsid,
tenglish,
tmath
--avgscore=(tEnglish+tMath)/2
from tblscore
order by (tEnglish+tMath)/2 desc ---------------------------------------------Group by 数据分组 --------------------------
--1.分组的目的,就是为了汇总、统计。================================================
--2.聚合函数。刚才所说的聚合函数其实就是把整个表中的数据作为”一组“,来进行统计汇总。
--聚合函数使用的时候一定会配合分组(gourp by )来使用,如果使用聚合函数时,没用分组,那么意义不大。
--聚合函数在使用的时候一定会分组,即便不写group by 语句,其实也是默认把整个表中的数据作为”一个组“来使用,进行统计. select * from TblStudent -- 当聚合函数与group by 分组语句一起使用的时候,其实这个聚合函数会将分组后的每一组的记录数据,进行聚合。
--group by 语句最终执行完毕后,分了几组,那么聚合函数会对每一组都进行聚合统计。
select
tsgender,
count(*)
from TblStudent
group by tsgender
MSSQL DBOtherSQL的更多相关文章
- [干货来袭]MSSQL Server on Linux预览版安装教程(先帮大家踩坑)
前言 昨天晚上微软爸爸开了全国开发者大会,会上的内容,我就不多说了,园子里面很多.. 我们唐总裁在今年曾今透漏过SQL Server love Linux,果不其然,这次开发者大会上就推出了MSSQL ...
- 分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节
1:MSSQL SQL语法篇: BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | vie ...
- MSSQL远程连接
背景:部署公司自己研发的ERP系统. 1)系统架构: .NET+MSSQL. 2)服务器系统:Windows Server 2008 R2 Enterprise 3)数据库:MSSQL Server ...
- 学习笔记 MSSQL显错手工注入
和朋友一起学习,速度就是快.感谢珍惜少年时. 网上很多都在长篇大论MSSQL显错手工注入,其实原理只有一小段.如下: ' and (查询一段内容)=1 and 'C'='Cnvarchar类型(查询一 ...
- MSSQL部分补丁的列表及下载地址(持续更新)
整理了MSSQL部分补丁的列表及下载地址(截至2016-11-18),供参考下. Edition Version Date Published Download Link SQL Server 201 ...
- .NET+IIS+MSSQL配置
好久没配置.NET+IIS+MSSQL了,跟以前不大一样了.总结下吧. 环境: Windows Server 2012 标准版 x64 SQL Server Express 2014 一.HTTP E ...
- C++-数据库【1】-C++连接MSSQL数据库
测试环境—— 系统:Win7 64bit 编译器:VC++ 2015 数据库:MSSQL 2008 R2 #include <Windows.h> #include <stdio.h ...
- mssql与mysql 数据迁移
概要: mssql向mysql迁移的实例,所要用到的工具bcp和load data local infile. 由于订单记录的数据是存放在mssql服务器上的,而项目需求把数据迁移到mysql ser ...
- 一起来测试天兔Lepus3.8 Beta版本的MSSQL部分
一起来测试天兔Lepus3.8 Beta版本的MSSQL部分 产品介绍:http://www.lepus.cc/下载地址:http://www.lepus.cc/soft/18手册地址:http:// ...
随机推荐
- BAT批处理(四)
网络命令 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" / ...
- php mongodb扩展 其他扩展也类似
MongoDBPHP 扩展 本教程将向大家介绍如何在Linux.window.Mac平台上安装MongoDB扩展. Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行 ...
- 【Docker 命令】- exec命令
docker exec :在运行的容器中执行命令 语法 docker exec [OPTIONS] CONTAINER COMMAND [ARG...] OPTIONS说明: -d:分离模式: 在后台 ...
- 【bzoj1708】[USACO2007 Oct]Money奶牛的硬币 背包dp
题目描述 在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统的货币系统中,硬币的面值通常是1,5,10,20或25,50,以及100单位的 ...
- POJ1236:Network of Schools——题解
http://poj.org/problem?id=1236 首先还是缩点,然后入度为0的点的个数就是你要投文件个数. 然后我们对于入度和出度为0的点的个数取最大值即为答案. (简单证明:入度和出度为 ...
- 洛谷 P2446 [SDOI2010]大陆争霸 解题报告
P2446 [SDOI2010]大陆争霸 题目背景 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭的神曾·布拉 ...
- React ref的用法
React的ref有3种用法: 1. 字符串(已废弃)2. 回调函数3. React.createRef() (React16.3提供) 1. 字符串 最早的ref用法. 1.dom节点上使用,通过t ...
- 深入JavaScript对象创建的细节
最近深入学习javascript后,有个体会:面向对象的方式编程才是高效灵活的编程,也是现在唯一可以让代码更加健壮的编程方式.如果我们抛开那些玄乎的抽象出类等等思想,我自己对面向对象的从写程序的角度理 ...
- python代码格式规范
目前的规范基于pep-0008 基本格式 缩进 使用4个空格进行缩进 行宽 每行代码尽量不超过80个字符 理由: 这在查看side-by-side的diff时很有帮助 方便在控制台下查看代码 太长可能 ...
- supervisor安装、配置和运行
supervisor是python写的进程管理工具,supervisor能够批量对进程执行启动,停止,重启等操作,有效提高了运维效率.注意supervisor只能管理前台进程,supervisor会自 ...