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 ll | a 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 null | a is null | 为null返回真 |
is not null | a is not null | 为not null返回真 |
between | a between b | 在a 和b之间为真 |
like | a like b | sql匹配,若a匹配到b,则结果为真 |
in | a 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