Msyql语句进阶
导出,导入数据库mysqldump
导出数据库 (备份数据库)
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
例:mysqldump -uroot -p123456 db1 >db1.sql
导入数据库 (还原数据库)
导入(方法一)
mysql -uroot -p123456 db1<db1.sql
方法二
进入到mysql数据库,添加sql文件,推荐使用此方法,安全
source /root/HA.sql
-B : 导出整个库包含建库语句
-A:导出全部数据库
把一个select的结果导出到文本
select * into outfile ‘/tmp/123.txt’ from t1;
是因为secure-file-priv 这个选项规定了导出的目录
打开my.cnf在文件末尾加入secure_file_priv=’'语句后
重启mysql。
逻辑运算符:
and or not
and 与
or 或
not 非
算术运算符:
= 等于
<> 不等于 !=
大于
< 小于
= 大于等于
<= 小于等于
in 运算符
语法如下:
WHERE column IN (value1,value2,…)
WHERE column NOT IN (value1,value2,…)
当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择。
例:找出价格是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,bname desc;
范围运算
between …and…
例:
查找价格在30到60之间(包含30和60)的书名和价格
select bname,price from books where price between 30 and 60;
Between and 可以使用>=和<=的方式来代替,并且使用大于小于意义表述更明确。
select bname,price from books where price >=30 and price <=
60;
模糊匹配查询
字段名 [not]like ‘通配符’ ----》% 任意多个字符
select bname from books where bname like ‘%程序%’;
加not 查询不包含程序的
MYSQL子查询
查询类型名称是“网络技术”的图书
先查询category表中网络技术类型的bTypeId
select btypeid from category where btypename=‘网络技术’;
然后在books表中查询bTypeName=7是哪本书
select bname,btypeid from books where btypeid=7;
嵌套查询:嵌套查询就是把两个select语句结合起来
select bname,btypeid from books where btypeid=(select btype
id from category where btypename=‘网络技术’);
查询类型名称为“黑客”的图书
select bname,btypeid from books where btypeid=(select btype
id from category where btypename=‘黑客’);