Select数据查询语言(DQL)

查询全部

select * from t_student

查询指定字段

select `student_no`,`student_name` from t_student

别名

select `student_no` as '学号',`student_name` as '学生姓名' from t_student

去除重复数据

select distinct * from t_result

查询系统版本

select version()

查询自增步长

select @@auto_increment_increment

使用表达式

select 100 * 10 - 5 as '计算结果'

And 条件查询

-- and
select * from t_resultwhere score >= 95 and score <= 100
-- &&
select * from t_resultwhere score >= 95 && score <= 100
-- between
select * from t_resultwhere score between 95 and 100
-- 以上三种方式效果相同查询95到100之间的成绩信息,包含95和100

Not 条件查询

select * from t_resultwhere student_no != 1000select * from t_resultwhere not student_no = 1000
-- 查询学号不等于1000的信息

Like 条件查询

select * from t_student where student_name like '王_'
-- 查询姓为‘王’开头,并且名字仅为2位的所有信息,_代表任一字符
select * from t_student where student_name like '王%'
-- 查询姓为‘王’开头的所有信息,%代表任意多个字符
select * from t_student where student_name like '%天%'
-- 查询所有姓名中包含‘天’的信息
-- 这里主要理解 % 和 _ 的作用,如有不明,可以去搜索一下

In 条件查询

select * from t_student where student_no in (1001,1002,1003)
-- 查询学号为1001,1002,1003的信息
-- in 同时可以和 % 和 _ 结合使用

Join On 联结查询

111

自连接

select a.`menu_name` as '父菜单', b.`menu_name` as '子菜单' from t_menu as a, t_menu as b where a.id = b.parent_id
-- 如果理解不了上面这句话,可以设计一个包含 id,name,parentid 三列的表,来对照一下,其中parentid一定是id的子集

分页(Limit)

select * from t_student limit 0,10
-- limit offset,
-- limit后面第一个参数代表从第第几个记录开始,后一个参数代表取多少个字段
select * from t_student limit 10, 10
-- 10,10 代表第二页
-- 因为每页显示10个记录,所以第 n 页为 limit (n-1) * pageSize, pageSize
-- 总页数 = 【数据总数 / pageSize】,采用向上整,比如5.1取整为6

子查询

-- 子查询的本质是 嵌套 查询,例如
select * from t_student 
where student_id in 
(select student_id from result where score >= 80)
-- 多重子查询(嵌套)
select * from t_studen 
where student_id in 
(select student_id from result 
	where score >= 80 and subject_id in (select subject_id from subject where subject_name = '高等数学-1')
)

Ceiling 和 Floor 函数

select ceiling(9.4)  
-- 向上取整,结果为10
select floor(9.4)
-- 向下取整,结果为9

Rand 函数

select rand()
-- 产生[0,1)之间随机数
select floor(rand() * 10 + 1)
-- 产生[1,10]之间随机整数

Char_length 函数

select char_length('hello')
-- 返回字符串长度

Concat(a, b) 函数

select concat('姓名:', student_name) as '学生姓名' from t_student
-- 将a和b进行连接

MD5 函数

insert user(username, password) values('admin', md5('123456'))
-- 验证
select count(*) from user where username = 'admin' and password = md5('12345')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值