--------------------------------------------基本常用查询--------------------------------------

自己简单练习做了个表。今天看了下hoojo大神的SQL Server T-SQL高级查询,我现在程度只能理解基础,所以就分享一下自己所联系的和理解的部分。

--查询所有
select * from [dbo].[Table]
--查询出所有名字
select all name from [dbo].[Table]
--过滤掉重复的性别
select distinct sex from [dbo].[Table]
--统计出表中有多少条信息
select count(*) from [dbo].[Table]
--统计出表中所有名字有多少条信息
select count(name) from [dbo].[Table]
--统计出过滤掉重复性别有多少条信息
select COUNT(distinct sex) from [dbo].[Table]
--排行前5名的姓名
select top 5 name from [dbo].[Table]
--如果要知道所有信息就将name替换成*----------------------
--列重命名(三种方式空格,as,单引号) --常用的是空格
select id 编号 , name as 姓名 , sex '性别' from [dbo].[Table]
--表重命名(俩种方式空格,as) --常用的是空格
select * from [dbo].[Table] as t
select * from [dbo].[Table] t
--列运算(俩列相加)
select (id+age) 总数 from [Table]
--将俩列合并显示在一列中用-隔开
select name +'-'+ age 个人信息 from [Table]
--where条件 (后面接列名 运算符 值)
select id 编号, name 姓名 from [Table] where id =2
select * from [Table] where id <=8
select * from [Table] where id >=2
select * from [Table] where id !=3
select * from [Table] where id !<7
select * from [Table] where id !>8
--将名字包含郑竹的信息不显示
select * from [Table] where name<>'郑竹'
--and 和 or 用法
--and 的用法是并且(满足所有条件)
select * from [Table] where id=3 and age=14
--or的用法是或者(满足其中一个条件)
select * from [Table] where age=13 or age=14
--between ...and...用法是俩者之间(显示条件中俩者之间的所有信息)
select * from [Table] where id between 2 and 8
--模糊查询(%替换所有输入字符,_替换一个输入字符) 关键字like替换运算符
select * from [Table] where name like '%'
select * from [Table] where name like '%电'
select * from [Table] where name like '_雨'
--子查询 关键字in (在条件之中)
select * from [Table] where id in (2,3,4,7)
select * from [Table] where name in('钱雨','李电','郑竹')
--not in (不在条件之中)
select * from [Table] where age not in (17,18,19)
select * from [Table] where name not in('钱雨','李电','郑竹')
--is null (显示条件中值为空的信息)
select * from [Table] where age is null
--is not null (显示条件中值不为空的信息)
select * from [Table] where age is not null
--order by 排序 (desc 降序,asc升序)
select * from [Table] order by age
select * from [Table] order by age desc
select * from [Table] order by age asc
--group by 分组
--(查询时将一列或n列分开查询并显示,查询条件一一对应后面group by)
select age from [Table] group by age
select id ,name, age from [Table] group by id,name, age
--按照年龄进行分组统计
select count(*) age from [Table] group by age
--按照性别进行分组统计
select count(*) sex from [Table] group by sex
--按照年龄和性别组合分组统计,并排序
select COUNT(*) age,sex from [Table] group by age,sex order by age
--按照性别分组,并且是id大于2的记录最后按照性别排序
select COUNT(*) id大于2的人数, sex from [Table] where id>2 group by sex order by sex
--查询id大于2的数据,并完成运算后的结果进行分组和排序
select COUNT(*) id大于2的人数,(age+id) 合计数 from [Table] where id>2 group by (age+id) order by (age+id)
--group by all
--按照年龄分组,是所有的年龄
select age from [Table] group by all age
--having 分组过滤条件
--按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息
select COUNT(*) 分组的条数, age from [Table] group by age having age is not null
--按照年龄和id组合分组,过滤条件是id大于1的记录
select id,age from [Table] group by id,age having id >1
--按照年龄分组,过滤条件是分组后的记录条数大于等于1
select COUNT(*) 分组后的记录条数, age from [Table] group by age having COUNT(*) >=1
--按照id和性别组合分组,过滤条件是id大于1,id的最大值大于2
select id,sex from [Table] group by id,sex having id>1 and max(id)>2

--------------------------------------------嵌套子查询--------------------------------------
--子查询就是内部查询或者说是内部选择,在子查询外部的是外部查询或者说是外部选择。
--这个是基础理论,我个人觉得子查询就是中心,外部查询就是外围,中心查询的语句都是基础语句演变而来,外部查询就是结合子查询一块查询结果
--下面就是简单的子查询格式
select * from /* 这个就是外部查询*/
(select id ,name ,age from [Table] where id >1) t /*内部查询*/
where t.age >15/* 这个就是外部查询*/
--内部查询中运用的语句就是基本常用查询语句
--外部查询可以包含基本常用查询语句
--例如where,group by,having,count,select查询,多个表或者视图的from语句

学习之路永无止境,不可好高骛远,也不可妄自菲薄,维持本心,方能获取更多知识。--W代码源

sql的基本查询语句的更多相关文章

  1. SQL结构化查询语句

    SQL结构化查询语句 SQL定义了查询所有关系型数据库的规则. 1.通用语法 SQL语句可以单行或者多行书写,以分号结尾 可以使用空格和缩进增强可读性 不区分大小写,但是关键字建议大写 3种注释 注释 ...

  2. SQL Server-简单查询语句,疑惑篇(三)

    前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开始正 ...

  3. SQL Server-简单查询语句,疑惑篇

      前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开 ...

  4. sql 中联合查询语句

    在查询语句中 两张表进行查询,可以通过 left join (左连接查询) :返回左表中的所有记录和右表中联结字段相等的记录  (意思就是左表中的数据会全部显示,右表中只会显示和左表中相等的字段) r ...

  5. c# sql在where查询语句中使用字符串变量与int型变量

    使用where语句访问数据库时where语句用上文中以及定义过的变量来查询. string sql3 = string.Format("update Ships set ContainerN ...

  6. sql sever基本查询语句

    查询(*可代表全部)(<>代表不等于于)select 列名 from 表名(,隔开)where 查询条件order by 排序的列名+连接的数据类型必须兼容(结果为字符串数据的连接 , 如 ...

  7. sql多条件查询语句

    如上图:三个文本可选项,那sql语句怎么写呢? 1.首先获取三个文本的值分别为Name,Age,Sex. 2.string sql="select * from 表 where 1=1&qu ...

  8. 数据库SQL优化分析查询语句总结

    方法一: SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_log ...

  9. sql server 2008 查询语句的红色波浪线

    在 Microsoft sql server management studio 里点击“编辑”——“IntelliSense”——“刷新本地缓存” 就会发现红色波浪线没了(前提是你的代码没错)

随机推荐

  1. Raphaël—JavaScript Library

    Raphaël-JavaScript Library What is it? Raphaël is a small JavaScript library that should simplify yo ...

  2. oracle获取某一字段字符串长度

    用length方法 select t.* from tp_area t where substr(t.area_id,0,2)='03' and length(t.area_id)>2

  3. JNI与JNA性能比较

    JNI与JNA性能比较 在介绍JNA时,提到了JNA是基于JNI的,是在JNI上封装了一层,JNI性能不如JNA.最近在网上看到篇简单的比较这两者性能的文档,感觉不错,现转载一下: 分别用JNI和JN ...

  4. Spting使用memcached

    applicationContext.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  5. unity 3d 生成apk文件时,设置图标

    如图所示:注意红线标注部分: 如果安装到手机之后,程序的图标还是unity的默认图标,重启手机即可.

  6. mysql 5.6

    mysql 5.6的安裝: 1,提前安装依赖的库环境. yum install -y  make bison cmake gcc-c++ ncurses ncurses-devel  git 2,下载 ...

  7. .NET中的CSV导入导出(实例)

    导入代码,从csv文件得到datatable         /// <summary>        /// Get Data From Csv File         /// (Th ...

  8. asp.net 常用的3中身份验证

    1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...

  9. Makefiles 介绍

    http://www-personal.umich.edu/~ppannuto/writings/makefiles.html Makefiles Makefiles (or, the GNU aut ...

  10. Category类别

    1.在已有类的基础上进行扩展,无需像继承一样子类化,就可以直接添加一些方法 2.继承不仅可以添加方法还可以添加属性,类别只能添加方法 3.类别不会改变现有类的方法,万一重写,自己写的优先级高 4.把类 ...