Mysql必知必会必知必会
查看有哪些库
show databases;
查看当前库中有哪些表
show tables
查看表中有哪些列
show columns from table_name;
describe table_name;
查看服务器状态
show status;
查看建表语句或者创建数据库的语句
show create table table_name
show create database database_name;
用来显示授权用户的安全权限
show grants
用来显示服务器错误或者警告
show errors
show warnings
limit语句
# 表示从第三行(包括)开始取值,取四条数据(mysql的行是从0开始的)
select id from table_name limit 3,4;
# Mysql5版本支持同样语义的写法:
select id from table_name limit 4 offset 3;
sql语句中使用全限定名称
select table_name.column_name from database_name.table_name;
order by 的说明
order by 多个列的时候,只有当第一个列相同的时候,才会根据第二个列进行排序,以此类推,但是如果第一个列是唯一的话,
那么就不会根据其他列进行排序
mysql在执行匹配的时候默认不区分大小写
# 这样可能查询出name='li'的结果
select id,name from table_name where name = 'Li';
判断是否为空或者不为空
select id from table_name where id is null;
select id from table_name where id is not null;
and的优先级大于or优先级
# 表示查询age 20)的数据
select id,name from table_name where age 20;
in和or的比较
in和or的作用一致,但是in比or更加清晰,而且一般来说in的效率比or高,而且in还可以包含子查询
使用通配符
%表示任意字符出现任意次数,比如select id from table_name where name like ‘jack%’;表示查询所有名称为jack开头的数据
通配符可以在任意位置并且可以使用多个通配符,值得注意的是%代表搜索模式中给定位置的0个、1个或者多个字符。
_通配符只匹配一个字符
不要过度使用通配符,因为它的效率不高,不要讲通配符放在匹配模式的开始处,因为这样搜索起来是最慢的,如果你在某个字段使
用了索引,然后使用通配符进行查询,如果将通配符放到开始处,那么就不会使用索引。不要把通配符放错位置了
评论0