查询涉及到的数据请查看:
Mysql系列课程–第三章 建表 插数据
一.数据库备份与还原
mysqldump -uroot -proot --databases yz > /home/test/yz.sql
备份多个数据库:
mysqldump -uroot -proot --databases yz myschool > /home/test/yz.sql
数据库还原:
mysql -uroot -proot < /home/test/JavaCourse/mysql/xiaoshuai.sql
二.自增主键的重新排序(不存在外键关联)
alter table student drop column s_no;
alter table student add column s_no int primary key auto_increment first;
三.普通查询
1.算术运算(as 起别名,可以省略)
select s_name,s_age*3+1 as '你能活到多大' from student;
select * from student where s_no = 2 + 1;
2.比较运算符
select * from student where s_sex <> '男';
select * from student where s_age > 30;
select * from student where s_age >=20 and s_age<=30;
select * from student where s_age between 20 and 30;
select * from student where s_age in (20,30,25);
select * from student where s_sex is null;
select * from student where s_name like '_大%';
select * from student where s_name regexp '.大';
3.逻辑运算符
select * from student where not s_sex='男';
select * from student where not s_sex='男' and s_age>20;
select * from student where s_age = 20 or s_age = 30;
select * from student where s_age != 20 xor s_age != 30;
4. in 与 or 的区别
没有区别,可以互换,当数据量小的时候用or,数据量大用in
select * from student where s_age in (20,30);
select * from student where s_age = 20 or s_age = 30;
5./找出20岁以上男人,20岁以下女人/
扫描二维码关注公众号,回复:
1462913 查看本文章
select * from student where s_sex='男' xor s_age<20;
select * from student where (s_sex='男' and s_age>=20) or (s_sex='女' and s_age<20);
select * from student where (s_sex='男' && s_age>=20) || (s_sex='女' && s_age<20);
(mysql支持 &&这样的查询方法 , 但是标准的sql是不支持的.)
四.字符串常用函数
1.连接字符串
select concat(s_name,'学生') from student;
2.字符串替换(位置从1开始)
select insert(s_name,1,2,'藏獒') from student where s_name='葬爱勇少';
3.转换成大写
select upper(s_name) from student where s_name= 'a';
4.首尾拼接
select lpad(s_name,7,'傻傻的') from student where s_name='葬爱勇少';
select rpad(s_name,7,'傻傻的') from student where s_name='葬爱勇少';
5.截取
select substring(s_name,1,2) from student where s_name='葬爱勇少';
6.字段去重
select distinct s_sex from student;
详细课程查询:
Mysql系列课程–第一章 Sql分类 常用命令
Mysql系列课程–第二章 约束 自增主键
Mysql系列课程–第三章 建表 插数据
Mysql系列课程–第四章 备份 普通查询
Mysql系列课程–第五章 高级查询 表连接 子查询 case when
Mysql系列课程–第六章 索引和视图
Mysql系列课程–第七章 触发器和存储过程
Mysql系列课程–第八章 sql精选35道题