查询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),‘‘))

时间: 2024-08-29 06:36:05

查询select的相关文章

使用explain查询select查询语句执行计划

1.使用explain查询select查询语句执行计划 mysql> select * from baba where name ='fjdsjf'; +------+--------+ | id   | name   | +------+--------+ |    1 | fjdsjf | +------+--------+ 查询该sql语句的执行计划 mysql> explain select * from baba where name ='fjdsjf' \G; **********

Oracle 查询(SELECT)语句(二)

?  简介 在前面的 Oracle 查询 SELECT 语句(一) 中介绍了 SELECT 常用的一些基本查询语法,接下来再来看 SELECT 更深入的一些查询功能和技巧,包括以下内容: 1.   All 与 Any 运算符 2.   分页查询(rownum) 3.   集合操作符(UNION.UNION ALL.INTERSECT.MINUS) 1.   All 与 Any 运算符 1)   All 运算符,表示满足给出列表中的所有值.通常用于以下场景: 1.   查出大于30号部门所有员工最

LINQ中in的实现方法-LINQ To Entities如何实现查询 select * from tableA where id in (1,2,3,4)

如果用in是字符串类型无问题,可以直接这样用 var result = SNFService.Instance.ModuleService.GetList(UserInfo).Where(entity => entity.DeletionStateCode == 0 ).Where(entity => urls.Contains((entity.NavigateUrl == null ? "" : entity.NavigateUrl).ToLower())).OrderB

mysql 查询select语句汇总

数据准备: 创建表: create table students( id int unsigned primary key auto_increment not null, name varchar(20) default '', age tinyint unsigned default 0, height decimal(5,2), gender enum('男','女','人妖','保密'), cls_id int unsigned default 0, isdelete bit defau

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:  3307 MYCAT 1.5 服务部署在宿主机上 MYCAT 192.168.31.207 :8806[SQL执行端口] / 9066[管理端口] 2 应用场景 2.0 MYCAT配置 schema.xml <schema name="TESTDB" checkSQLschema=&quo

SQL_2_查询Select语句的使用

查询一词在SQL中并不是很恰当,在SQL中查询除了向数据库提出问题之外,还可以实现下面的功能: 1>建立或删除一个表 2>插入.修改.或删除一个行或列 3>用一个特定的命令从几个表中查找所需要的信息并返回 4>改变信息的安全性 现在开始学习SELECT语法: SQL语句对大小写并不敏感,但在数据库中的数据却是大小写敏感的. SQL语句出现分号就意味着本条语句已经结束. SQL中自动对数字采用右对齐,对字符类型采用左对齐. SELECT payee,remarks,amount,ch

sql: 查询,select

快速查询数据库中拥有那些表项:(类似linux中的ls, ls |grep table_name*)select * from tab where tname like 'YOUR_QERYNAME%%'; TNAME TABTYPE CLUSTERID ----------------------------------------- YOUR_QERYNAME TABLE YOUR_QERYNAME_xxx TABLE YOUR_QERYNAME_yyyy TABLE ....

MySQL查询——select

SELECT select的完整语法: select col1, col2,... # 业务查询的字段 from table_name # 选取的哪张表 [where single_conditions] # single_conditions条件表达式,个体约束(条件) [[group by column_name1] # column_name1以哪个字段名分组 [having group_conditions]] # group_conditionds条件表达式,分组约束 [order b

SQL Server中的分页查询 select top

SQL Server中的分页查询 https://blog.csdn.net/tswc_byy/article/details/82053091 零.码仙励志 比我差的人还没放弃,比我好的人仍在努力,我就更没资格说我无能为力 一.建库和建表 create database scort use scort create table emp ( empno int primary key, ename nvarchar(10), sal int, deptno int ) insert into e