MySQL语句进阶(2)

导出,导入数据库mysqldump
mysqldump工具可以实现数据库的备份和还原。
导出数据库:
格式:mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u root -p123456 HA>HA.sql
导入数据库
方法1:mysql -uroot -p123456 HA2 < HA.sql
方法2:use HA2;
source /root/HA.sql
拓展:Mysqldump -uroot -p123456 -B 库名>文件.sql
-B : 导出整个库包含建库语句
-A:导出全部数据库
把一个select的结果导出到文本
语法:select * into outfile ‘/tmp/123.txt’ from HA.students;
执行这个命令可能会报错
解决方法:vim /etc/my.cnf
在上面的文件最后一行添加secure_file_priv=’’
然后保存退出重启服务:/etc/init.d/mysqld restart
然后进到数据库,
重新执行select * into outfile ‘/tmp/123.txt’ from HA.students;
Sql语句进阶:
导入数据库:
mysql -e ‘create database book’ -uroot -p123456
mysql -uroot -p123456 book<book_utf8.sql
导入之后查看一下
逻辑符运算 and 与 or 或 not 非
查询书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格
select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
算术运算:= 等于
<> 不等于 !=

大于
< 小于
= 大于等于
<= 小于等于
In 运算符
IN 运算符用于 WHERE 表达式中,在前面加not运算符的意思是反向运算
找出价格大于60的书籍
select bName,price from books where price>60;
找出价格等于60的书籍
select bName,price from books where price=60;
找出价格不等于60的 书籍
select bName,price from books where price<>60;
找出价格是60,50,70的记录
select bName,price from books where price in (50,60,70);
找出价格不是60,50,70的书籍
select bName,price from books where price not in (50,60,70);
排序
升序:order by “排序的字段” asc 默认
降序:oredr by “排序的字段” desc
select bName,price from books where price in (50,60,70) order by price asc;
select bName,price from books where price in (50,60,70) order by price desc;
多个字段排序
select bName,price from books where price in (50,60,70) order by price desc,bName desc;
对比单字段的排序
select bName,price from books where price in (50,60,70) order by price desc;
范围运算between …and…
查找价格在30到60之间(包含30和60)的书名和价格
select bname,price from books where price between 30 and 60;
查找价格不在30到60之间的书名和价格
select bName,price from books where price not between 30 and 60 order by price desc;
模糊匹配查询
[not]like 像或不像 %任意多个字符
查找书名中包括"程序"字样记录
select bName from books where bName like ‘%程序%’;
查找书名中不包括"程序"字样记录
select bName from books where bName not like ‘%程序%’;
MYSQL子查询
子查询就是嵌套查询,多一个select查询条件,就是把两个select查询结合起来
查询类型名称是“网络技术”的图书(嵌套查询)
select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=‘网络技术’);
查询类型名称为“黑客”的图书(嵌套查询)
select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=‘黑客’);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值