查询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( ...
随机推荐
- matlab练习程序(PCA<SVD>)
clear all;close all;clc;img1=imread('Corner.png');img2=imread('Corner1.png');img3=imread('Corner2.pn ...
- OC省字典的数组摘要集
开放式党员 NSString *filePath = @"/Users/dlios/Downloads/area.txt"; 推断错误值 打印出来 NSError *error = ...
- 在SurfaceView中自由书写和擦除
/////////继承SurfaceView 的类 public class PaintView extends SurfaceView implements Runnable,SurfaceHold ...
- linux下一个Oracle11g RAC建立(四)
linux下一个Oracle11g RAC建立(四) 三.配置共享存储 配置ASM管理准备 1)OCRDISK :存储CRS资源配置信息 2)VOTEDISK:仲裁盘.记录节点状态 3)DataDis ...
- 使用C++实现功能下载文件
今天问一个同学C++实现的下载链接下载并保存给定的文件,互联网搜索.看到这样的事情在网上.因此,改变下直接带来,因为他的代码是在VC++,我导入到VS2010中出现点小问题.所以改了下贴了个VS中亲測 ...
- MVC 快速开发框架
ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets jqwidgets.js: 是一个功能完整的框架,它具有专业的 ...
- TortoiseGit安装与配置(转)
TortoiseGit 简称 tgit, 中文名海龟Git. 海龟Git只支持神器 Windows 系统, 有一个前辈海龟SVN, TortoiseSVN和TortoiseGit都是非常优秀的开源的版 ...
- 第23章 访问者模式(Visitor Pattern)
原文 第23章 访问者模式(Visitor Pattern) 访问者模式 导读:访问者模式是我个人认为所有行为模式中最为复杂的一种模式了,这个模式可能看一遍会看不懂,我也翻了好几个例子,依然不能很好的 ...
- Cocos2d-X采用CCScrollView创建滚动视图
CCScrollView滚动视图可以让游戏有效果,并能够通过滚动视图切换游戏场景,滚动视图通常用来选择在游戏中的级别 实例1:使用CCScrollView创建一个简单的滚动视图 首先创建一个Scrol ...
- crawler_爬虫_反爬虫策略
关于反爬虫和恶意攻击的一些策略和思路 有时网站经常受到恶意spider攻击,疯狂抓取网站内容,对网站性能有较大影响. 下面我说说一些反恶意spider和spam的策略和思路. 1. 通过日志分析来 ...