查询select
--------------select查询-----------------
--查询所有信息(方法一)
select * from stuinfo --*号代表所有列
--查询所有信息(方法二)
select StuNo,StuName,StuAge,StuSex,address from stuinfo
--查询单列
select StuName from stuinfo
--查询多列
select StuNo,StuName from stuinfo
--将多列组合成一列
select StuName+'_'+address as 姓名_地址 from stuinfo
--查询中为列名取别名
--方法一
select StuNo as 学号,StuName as 姓名 from stuinfo
--方法二
select StuNo 学号,StuName 姓名 from stuinfo
--方法三
select 学号=StuNo,姓名=StuName from stuinfo
--查询值空的行
select * from StuInfo where address is NULL
--注意:判断为空用 is NULL而不是 =NULL
--查询值不为空的行
select * from StuInfo where address is not NULL
--常量列
select 问候='你好'
select 21.5
select 学号=StuNo,姓名=StuName,学院='硅谷学院' from stuinfo
--topN查询(限制查询行显示结果)
--以行来筛选
select top 2 * from StuInfo where StuAge>21
--以百分比来筛选
select top 25 percent * from StuInfo
--消除重复行查询
select distinct StuSex from StuInfo--消除性别重复的行
select distinct StuName,StuSex from StuInfo--消除性别和姓名都重复的行
--between查询条件(判断某个区间段)
select * from StuInfo where StuAge between 21 and 23
--注意:StuAge between 21 and 23相当于StuAge>=21 and StuAge<=23
--in查询条件(判断是否在指定的集合中)
select * from StuInfo where StuAge in(21,23)
--and条件查询(左右两边的条件为真结果才为真)
select * from StuInfo where StuName='张三' and StuAge=21
--or条件查询(左右两边的条件只要有一个为真结果就为真)
select * from StuInfo where StuName='张三' or StuAge=21
--模糊查询
select * from StuInfo where StuName like '张%'
---------------order by排序---------------------
--升序(asc 默认)
select * from StuInfo order by StuAge
--注意:排序中如果没有加asc、desc,默认就为asc
--降序(desc)
select * from StuInfo order by StuAge desc
--多字段排序
select * from StuScore order by score desc,StuNo desc
--注意:多字段排序时,会先对第一个字段进行排序,如果第一个字段无法排序的行就使用第二个字段进行排序,以此类推...
--只能查出一行数据
select top 1 * from StuInfo order by StuAge
--with ties能显示并列的行
select top 1 with ties * from StuInfo order by StuAge
------------------分组查询---------------------
--查询每门课程的总分(课程名,总分)
select subject,sum(score) from
StuScore group by subject
--查询每位同学的总分(学号,总分)
select StuNo,sum(score) from
StuScore group by StuNo
--注意:分组查询中,字段能够出现在select后面的情况:
--1、该字段有出现在group by后面
--2、该字段出现在聚合函数中
--3、常量列
select StuNo,sum(score),'sve' as 常量列 from
StuScore group by StuNo
--查询每位学员有进行几次考试
select StuNo,count(*) from StuScore
group by StuNo
--查询出没有缺考的学号
select StuNo,count(*) from StuScore
group by StuNo having count(*)>2
--查询出没有缺考并且有通过考试的学号
select StuNo,count(*) from StuScore
where score>=60
group by StuNo having count(*)>2
order by StuNo desc
--关键字顺序:where->group by->having->order by
--where:先对数据集进行条件筛选
--group by:对剩下来的数据进行分组
--having:对分组后的结果再次进行筛选
--order by:进行排序
----------------多字段分组---------------------
--查询有参加补考的学号、科目、考试次数
select StuNo as 学号,subject as 科目,count(*) as 考试次数 from StuScore
group by StuNo,subject having count(*)>1
----------------修改卡密码---------------------
--方法一
update Card set PassWord=replace(PassWord,'o','0')
update Card set PassWord=replace(PassWord,'i','1')
--方法二
update Card set PassWord=replace(replace(PassWord,'o','0'),'i','1')
------------------排序---------------------
select left(num,charindex('-',num,1)-1) from Card
select right(num,len(num)-charindex('-',num,1)) from Card
--方法一
select * from Card order by
convert(int,left(num,charindex('-',num,1)-1)),
convert(int,right(num,len(num)-charindex('-',num,1)))
--方法二
select * from Card order by
convert(int,left(num,charindex('-',num,1)-1)),
convert(int,stuff(num,1,charindex('-',num,1),''))
查询select的更多相关文章
- sql_查询select
sql_查询select /****** Script for SelectTopNRows command from SSMS ******/ [r_gonghao] ,[r_mingzi] , ...
- mysql数据库(二):查询(SELECT)
一. 数据库查询—查询(SELECT) 单表查询 多表联合查询 二. 查询—单表查询 查询特定字段: select <字段1,字段2,...> from <表名>; 示例:查询 ...
- Mybatis查询select 传单个参数不识别,找不到
今天, Mybatis查询select 传单个参数不识别,找不到 解决办法: 加上jdbc=varchar #{XXX,jdbc=VARCHAR}
- Oracle 查询(SELECT)语句(一)
Ø 简介 本文介绍 Oracle 中查询(SELECT)语句的使用,在 SQL 中 SELECT 语句是相对内容较多的,也是相对比较复杂一点的,所以这里拿出来单独学习. 首先,我们先来理一下思路,我 ...
- HQL查询——select子句
select子句 select子句用于选择指定的属性或者直接选择某个实体,当然select选择的属性必须是from之后持久化类包含的属性: select p.name from Person as p ...
- MyCat 学习笔记 第十一篇.数据分片 之 分片数据查询 ( select * from table_name limit 100000,100 )
1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150: 330 ...
- Microsoft SqlServer2008技术内幕:T-Sql语言基础-读书笔记-单表查询SELECT语句元素
1.select语句逻辑处理顺序: FORM WHERE GROUP BY HAVING SELECT OVER DISTINCT TOP ORDER BY 总结: 2.FORM子句的表名称应该带上数 ...
- sql: 查询,select
快速查询数据库中拥有那些表项:(类似linux中的ls, ls |grep table_name*)select * from tab where tname like 'YOUR_QERYNAME% ...
- mysql 查询select语句汇总
数据准备: 创建表: create table students( id int unsigned primary key auto_increment not null, name varchar( ...
随机推荐
- Properties类读写.properties配置文件
package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...
- iOS:删除小程序
//Applet的批次从父视图中移除 NSArray *subViews = [_scrollView subviews]; if([subViews count] != 0) { [subViews ...
- krpano漫游加方向性3D声音(这篇文章已被移到krpano中国网站 krpano360.com)
需求: 在场景转换视角时.会出现不同方位的声音以及对应的音量变化,也即是将声音视作hotspot.当视角转到该声音热点时,也随之听到声音.官方样例:点击打开链接 本文已经搬迁到下述网址.请点 ...
- XML DTD详解(转)
前情提要与本文内容介绍 前面的两篇XML相关博文: 第一篇是介绍格式正规的XML: 格式正规的XML:语法 属性 实体 处理指令 样式单 CDATA节 第二篇介绍DTD,引入有效的XML的概念(符合语 ...
- python基础课程_学习笔记21:文件和材料
文件和材料 打开文件 open功能是用来打开文件,语法例如,下面的: open([name[,mode[,buffering]]) open函数使用一个文件名称作为唯一的强制參数,然后返回一个文件对象 ...
- java maven quartz exampe 实用指南
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- springMVC项目异步错误处理请求Async support must be enabled on a servlet and for all filters involved in async
离github在down下一个项目,springMVC-chat.总体上有标注.这就是零配置. 这可苦了我,费尽周折,最后整合到项目现在看起来有点.出来以下的错误.红色部分.解决方法为,在web.xm ...
- C# Windows服务的创建、安装、调试
一.查看已有的Windows服务 选择菜单"开始"-〉"控制面板"-〉"管理工具"-〉"服务"来查看现有系统中的服务 二 ...
- Android中的“再按一次返回键退出程序”实现[转]
用户退出应用前给出一个提示是很有必要的,因为可能是用户并不真的想退出,而只是一不小心按下了返回键,大部分应用的做法是在应用退出去前给出一个Dialog,我觉得这样不太友好,用户还得移动手指去按dial ...
- Scrapy研究和探索(七)——如何防止被ban大集合策略
说来设置的尝试download_delay少于1,不管对方是什么,以防止ban策略后.我终于成功ban该. 大约scrapy利用能看到以前的文章: http://blog.csdn.net/u0121 ...