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:// ...
随机推荐
- TCP系列38—拥塞控制—1、概述
在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...
- 个人作业4 alpha阶段 个人总结
一.个人总结 二.回答问题 三.再提问题 Q1:关于第三章过早优化 过早优化:既然软件是"软"的,那么它就有很大的可塑性,可以不断改进.放眼望去,一个复杂的软件似乎很多的模块都可以 ...
- AutoResetEvent的基本用法
The following example uses an AutoResetEvent to synchronize the activities of two threads.The first ...
- SQL SERVER技术内幕之10 可编程对象
一.变量 变量用于临时保存数据值,以供在声明它们的同一批处理语句中引用.例如,以下代码先声明一个数据类型为INT的变量@i,再将它赋值为10; DECLARE @i as INT; SET @i = ...
- java 基础--数组--004
1,数组定义格式 String[] aa; String aa[];2,二位数组的定义格式1 String aa[][] = new String[m][n]; String[] aa[] = new ...
- get computer system mac info in javascript
get computer system mac info in javascript Q: how to using js get computer system mac information? A ...
- 【ADO.NET】SqlBulkCopy批量添加DataTable
使用事务和SqlBulkCopy批量插入数据 SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据 ...
- c/c++中的关键字(static、const、inline、friend)
static:1.a.c语言中static修饰的局部变量在编译时赋初始值,只赋初始值一次,在函数运行时已有初值,每次调用函数时不用重新赋值,指示保留上次 函 数调用结束时的值. 如果定义局部变量不赋初 ...
- hdu 2962 Trucking (最短路径)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 安装单机Hadoop系统(完整版)——Mac
在这个阴雨绵绵的下午,没有睡午觉的我带着一双惺忪的眼睛坐在了电脑前,泡上清茶,摸摸已是略显油光的额头(笑cry),,奋斗啊啊啊啊!!%>_<% 1.课程回顾. 1.1 Hadoop系统运行 ...