好记性不如烂笔头,每次看了忘,忘了看,这次做一个记录!
查看存在的数据库
mysql > show databases;
转到自己需要查看的数据库
mysql > use 数据库名;
查看数据库中的表
mysql >show tables;
显示表列
mysql >show columns from 表名;
从example表中,检索名为id的列(输出结果的顺序是杂乱无章的)
mysql >select id from example;
从example表中,检索名为id的列,title的列,time的列
mysql >select id , title ,time from example;
time这一列的日期很多都是重复的,现在只需要每个日期返回一次,列名前加关键字distinct
mysql >select distinct time from example;
限制输出五行,限制加关键字:limit 数字 (显示的结果是select的基础上限制的,select返回的是无序的,第一行是0)
mysql >select id from example limit 5;
限制输出的从XX到XX行,如,限制输出搜索结果的第2到8行
mysql >select id from example limit 2,8;
排序输出关键字:order by 列名,列名,列名(前边的检索结果将会按照by后边的列名排序输出 )默认升序
mysql >select id from example order by id; #按照id排序输出
mysql >select id from example order by time;#按照time排序输出
mysql >select id , title ,time from example order by id , title ;
#检索三列,按照前两列结果排序,先按照id排序,然后按照tilte排序
降序排列,关键字:desc ,想降序哪个字段就写在那个字段后边
mysql >select id , title ,time from example order by id desc, title ;
#搜索结果按照id降序排序,降序对title无效
过滤条件搜索,加关键字where。
如果同时使用order by和where,order by 在where的后边
where =" 字符串" ,字符串不区分大小写
#搜索id = 18的有关字段
mysql >select id , title ,time from example where id = 18;
#可以用表中任意的字段通过where字段顾虑
mysql >select id , title ,time from example where name = "hahahha"
#id为1到20之间的数据
mysql >select id , title ,time from example where id between 1 and 20;
#返回time为空的
mysql >select id , title ,time from example where time IS NULL;
where
and操作符 /or操作符
and操作符的优先级大于or,因此,设置条件时最好加括号
每额外添加一条过滤语句就增加一个and或者or
#id小于18 且 time等于time = "2019-01-01"的数据
mysql >select id , title ,time from example where id < 18 and time = "2019-01-01";
#id小于18 或者 time等于time = "2019-01-01"的数据
mysql >select id , title ,time from example where id < 18 or time = "2019-01-01";
in操作符
in (限定,限定,限定),就是限定过滤的条件范围,范围就在in后边的括号里
比如:
“where id in(1001,1002) 等价于:where id = 1001 and id =1002
#输出id为1,2,3的数据
mysql >select id , title ,time from example where id in (1,2,3);
not操作符 匹配除了后边的条件的