JAVA-MySQL四{MySQL重点DQL查询数据}JAVA从基础开始 --7

DQL查询语句

DQL - Data Query Language:数据查询语言

所有查询操作都用它 Select
简单查询,复杂查询都能做
数据库最核心语言
使用最多

指定查询字段

1.全查

SELECT * FROM `student`

在这里插入图片描述

2.指定字段查询

SELECT `gradeid`,`name` FROM `student`

在这里插入图片描述

3.别名,给结果起名字 AS

SELECT
	`gradeid` AS 年级,
	`name` AS 名字 
FROM
	`student`

在这里插入图片描述
也可以给表起别名

SELECT
	`gradeid` AS 年级,
	`name` AS 名字 
FROM
	`student`  AS s

4.可以有函数

concat

SELECT
	concat( '年级:', `gradeid`, '年级' ) AS 年级,
	concat( '姓名:', `name` ) AS 名字 
FROM
	`student` AS s

在这里插入图片描述

去重(重要!)-distinct

–查询有成绩的学生

-- select `studentName` from  `exam`
-- 发现重复数据
select distinct `studentName` from  `exam`


在这里插入图片描述
后:
在这里插入图片描述

结果:重复数据只显示一条

–查询系统版本号

在这里插入图片描述
– 运算

在这里插入图片描述

Sql代码

 select `studentName` from  `exam`
-- 发现重复数据
select distinct `studentName` from  `exam`

-- 查询系统版本号
select version()
-- 运算
select 100*4/5+3 as 运算
-- 查询自增的步长
select @@auto_increment_increment

select   `studentName`,`examFraction` +1 as '提分后' from `exam`

表格式:文本值、列、NULL、函数、计算表达式、系统变量
select 表达式 from 表

where条件子句

作用:检索数据中符合条件的值

逻辑运算符:与-或-非
运算符语法描述
and &&a and b a&&b与 两真为真
or lla or b allb或 一真为真
not !a not b !b非 真-假 假-真
select `studentname` ,`examFraction` from `exam`
where `examFraction` >= 80

在这里插入图片描述
模糊查询(区间)

-- 模糊查询
select `studentname` ,`examFraction` from `exam`
where `examFraction` BETWEEN 90 AND 100

在这里插入图片描述
除去什么之外 not !=

-- 除了1号的学生之外的成绩
select `studentname` ,`examFraction` from `exam`
# where `studentname` !=1
where not `studentname` =1

在这里插入图片描述

模糊查询:比较运算符(重点)
运算符语法描述
is nulla is null为null返回真
is not nulla is not null为not null返回真
betweena between b在a 和b之间为真
likea like bsql匹配,若a匹配到b,则结果为真
ina in(a1,a2,a3)若a为 a1,a2,a3其中一个 结果为真

代码

-- 模糊查询
-- 查询李姓同学
-- like
-- 开头是李的 %百分号代表任意字符
select `id`,`name` from `student`
where `name` like '李%'
-- 李后面一个字的  用下划线_  一个字一个_
select `id`,`name` from `student`
where  `name` like '李_'
-- 名字中间有a的同学
select `id`,`name` from `student`
where  `name` like '%a%'
-- in
-- 查询指定
select `id`,`name` from `student`
where id in (1,5,11) -- 查询id是1,5,11的数据
-- in 是具体的一个或者多个值,不能使用% _ 的模糊查询

-- 查询空  is null   或 =''
select `id`,`name`,`address` from `student`
where `address` is null
-- 查询不为空
select `id`,`name`,`address` from `student`
where `address` is not null

查询章节较长,分下章开始讲联表查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值