查询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( ...
随机推荐
- 浙江大学PAT考试1069~1072(2013-11-2)
11 题目地址:http://pat.zju.edu.cn/contests/pat-a-practise 1069: 由下降序和上升序两个四位数不断相减,然后得到新数据,始终会到达一个数字终止. 递 ...
- TextView——setCompoundDrawables说明
Drawable drawable = mContext.getResources().getDrawable(R.drawable.duringtime); drawable.setBounds( ...
- zoj3791(An Easy Game) DP
意甲冠军:给定两个01弦s1,s2.每一个变化s1在m字 - 位.要求k制作步骤之后s1变s2有多少种方法. 解法:DP,关键是状态的设计.考虑还是唯一性和可传递性.dp[i][j]表示第i步后有j个 ...
- 组合数处理(逆元求解)...Orz
网上发现了不错的博客讲解... 熊猫的板子:http://blog.csdn.net/qq_32734731/article/details/51484729 组合数的预处理(费马小定理|杨辉三角|卢 ...
- HDU 2048 号码塔(DP)
号码塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- [Python 2.7] Hello World CGI HTTP Server
# CGI HTTP server ## Getting Started Python 2.x is preferred to this simple demo. I'm using Python 2 ...
- Codeforces 328B-Sheldon and Ice Pieces(馋)
B. Sheldon and Ice Pieces time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Cocos2d-x3.0之路--02(引擎文件夹分析和一些细节)
关于怎么搭建好开发环境的我就不写了,网上非常多. 那么 我们来看看 引擎文件的文件夹 所谓知己知彼 百战不殆嘛 先说一下setup.py 这个文件是有关配置的python文件,比方我们在进行andro ...
- Hibernat之关系的处理一对一处理
第一步:编写两个pojo,比如一个学生表一个班级表 这里使用注解. 需要 公司表: package com.qcf.pox; import javax.persistence.CascadeType ...
- DB2常用sql demo
.查找员工的编号.姓名.部门和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd. ),birthday,),'日期不详') birthday from em ...