zoukankan      html  css  js  c++  java
  • sql的基本查询语句

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

    自己简单练习做了个表。今天看了下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语句

  • 相关阅读:
    关于产品的一些想法
    js中this的问题
    C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?)
    SSL握手中win xp和SNI的那点事
    VMware安装GHOST版XP不成功的解决
    Inno Setup入门(十二)——Pascal脚本(1)
    VS打包软件部署------ClickOnce应用安装 (各版本.net引导文件安装,再发布文档离线安装下载地址)
    利用VS自带的命令行工具查看和生产PublicKeyToken
    获取批处理文件所在路径
    Nginx Windows详细安装部署教程
  • 原文地址:https://www.cnblogs.com/W--Jing/p/5923402.html
Copyright © 2011-2022 走看看