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:// ...
随机推荐
- Swift-元祖
1.元组是多个值组合而成的复合值.元组中的值可以是任意类型,而且每一个元素的类型可以是不同的. let http404Error = (,"Not Found") print(ht ...
- .net Mvc4 View—布局页与分部页
▲ 一.Layout属性 1.1.@RenderPage():渲染制定的页面到占位符. 注意:@RenderPage()可以使用多次,这点与@RenderBody()不同 ...
- Thinkphp5的使用phpmailer实现发邮件功能(163邮箱)
Thinkphp5本身并没有实现发邮件的功能,至少据我所知. 本文利用网易邮箱作为发邮件的邮箱.作为发送邮件的前提是需要开启SMTP服务,打开网易邮件,点击设置按钮,如下图所示 勾选smtp服务 保存 ...
- 用svmpredict输出的结果为空
源程序:
- 【bzoj2870】最长道路tree 树的直径+并查集
题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...
- 【bzoj5108】[CodePlus2017]可做题 拆位+乱搞
题目描述 给出一个长度为 $m$ 的序列 $a$ ,编号为 $a_1\sim a_m$,其中 $n$ 个位置的数已经确定,剩下的位置的数可以任意指定.现在令 $b$ 表示 $a$ 的前缀异或和,求 $ ...
- BZOJ4821 SDOI2017相关分析(线段树)
纯粹的码农题.维护x的和.y的和.xy的和.x2的和即可.可能会炸long long. #include<iostream> #include<cstdio> #include ...
- python函数调用关系图(python call graph)
由于要重构项目的部分代码,要整理好主要的函数调用关系,不想自己看代码慢慢画出结构,想找出一种通用的,节省人力的方法得出函数间的调用关系图,于是发现以下几个工具.(内网没装好graphviz,还没真正用 ...
- WC2018集训 吉老师的军训练
WC2018集训 吉老师的军训练 #include<bits/stdc++.h> #define RG register #define IL inline #define _ 20000 ...
- 122. Best Time to Buy and Sell Stock II (Array)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...