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:// ...
随机推荐
- Python实现FTP服务功能
本文从以下三个方面, 阐述Python如何搭建FTP服务器 一. Python搭建FTP服务器 二. FTP函数释义 三. 查看目录结构 四. 上传下载程序 一. Python搭建FTP服务器 1. ...
- QT分析之消息事件机制
原文地址:http://blog.163.com/net_worm/blog/static/127702419201001432028526/ 上回我们分析到QPushButton的初始化,知道了Wi ...
- asp.net mvc4中Json的应用
做一个简单的 Json实例,从页面获取后台的Json数据 1.控制台: public class HomeController : Controller { // // GET: /Home/ pub ...
- 【zoj2314】Reactor Cooling 有上下界可行流
题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...
- 前端基础:CSS样式选择器
前端基础:CSS样式选择器 CSS概述 CSS是Cascading Style Sheets的简称,中文意思是:层叠样式表,对html标签的渲染和布局.CSS规则由两个主要的部分组成:1.选择器:2. ...
- [洛谷P5075][JSOI2012]分零食
题目大意:有$m(m\leqslant10^8)$个人站成一排,有$n(n\leqslant10^4)$个糖果,若第$i$个人没有糖果,那么第$i+1$个人也没有糖果.一个人有$x$个糖果会获得快乐值 ...
- Android ListView的优化
最近的项目中有通讯录这个模块,里面的通讯录涉及的联系人数量很大,导致在加载页面的时候有点卡,所以就必须得进行优化,优化的最终实现理论是什么?就是让ListView一次性加载的数据较少,后续根据用户操作 ...
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- bzoj4773: 负环(倍增floyd)
浴谷夏令营例题...讲师讲的很清楚,没看题解代码就自己敲出来了 f[l][i][j]表示i到j走2^l条边的最短距离,显然有f[l][i][j]=min(f[l][i][j],f[l-1][i][k] ...
- 51nod 1296 有限制的排列(DP)
对于一个i,如果要比邻居大,那么i比i-1大,i+1比i小,比邻居小同理.设v[i]=0表示i与i-1的关系无限制,v[i]=1表示a[i-1]>a[i],v[i]=2表示a[i-1]<a ...