show databases;
use mysql;
show tables;
select database(); #查询当前使用的数据库
select version(); #查询数据库的版本号 (在命令终端执行mysql --version也可以获取)
desc mysql.user;#查看表的全部字段
show create table mysql.user\G; #显示更详细的建表语句
select user(); #查询当前用户
create database test;
use test;
grant all on *.* to "user1"@"localhost" identified by ‘123456‘; #创建一个用户并授权;
**************************************************************************************************
create table biao_name(`name` varchar(20),`id` int(10),`phone_num` varchar(20));#创建一个表
insert into biao_name value("zhangsan","1","12345678"); #向表中增加数据
select * from biao_name;#查询表内容
delete from biao_name;#删除表的数据
update biao_name set name=‘lisi‘ where name="zhangsan"; #修改表的内容
truncate table biao_name;#清空表的数据
drop table biao_name; #删除表
**************************************************************************************************
alter table biao_name add sex varchar(20);#增加表的字段
alter table biao_name drop sex; #删除表字段
alter table biao_name change id ID varchar(20);#修改表的字段
desc mysql.user;#查看表的全部字段
**************************************************************************************************
一、基础查询
#语法:select 查询列表 from 表名;
1.查询表中的单个字段
select user from mysql.user;
2.查询表中的多个字段
select user,authentication_string from user;
3.查询所有字段
select * from user\G;
#用``来标识字段而非关键字
select `plugin` from user;
4.查询常量值
select 100;
select ‘number‘;
5.查询表达式
select 100*90;
6.查询函数
select version();
7.起别名:使用as
方式一:select 99*98 as ‘结果‘;
方式二:select user ‘账号‘,authentication_string ‘密码‘ from user;
别名中有特殊符号、空格等需要加双引号或者单引号
8.去重:distinct
select distinct user as ‘账号‘,authentication_string as ‘密码‘ from user;
9.“+”号的作用:只能作运算符
select 100+90;
select ‘123‘+100;#将字符123转换成数值,再作加法运算
select ‘join‘+90;#转换失败,则为0再作加法运算
select null+90;#只要其中一方为null,则结果为null
10.拼接:concat()函数
select concat(‘a‘,‘1‘,2) as ‘结果‘;
select concat(user,authentication_string) as "账号*密码" from user;
select concat(null,123);#与null拼接为null
11.不为空:ifnull()函数
select concat(‘jion‘,ifnull(null,0));
12.为空:isnull()函数
判断某个字段或者表达式是否为null,如果是,则返回1,否则为0;
select isnull(phone_num) from biao_name;
**************************************************************************************************************
二、条件查询
#语法:select 查询列表 from 表名 where 筛选条件;
(1).条件运算符:> < = != <> >= <= 其中=,<>号无法判断null值
select user from user where user=‘user1‘;
select `user` from user where user<>‘user1‘;
(2).逻辑运算符:&& || ! and or not
select user,authentication_string from user where user!=‘user1‘ and user like "mysql%";
(3).模糊查询:like
一般与通配符使用 % :任意多个字符,包括0个字符 _ :任意单个字符
between and
in 类似于等于,不支持通配符
is null
select user,authentication_string from user where user like "__s_l%"; #查询第三个字符为s,第5个字符为l的用户账号和密码
注意转义字符\ 或者escape
select plugin from user where plugin like "_____\_%";
或者:select plugin from user where plugin like "_____$_%" escape ‘$‘; #用escape指定$为转移符
select user,authentication_string from user where user between ‘a‘ and ‘z‘ ; #包含临界值,不能调换顺序>=‘a’ <=‘z’
select user,authentication_string from user where user in (‘root‘,‘user1‘); #查找user字段为root或者user1的信息
select user,authentication_string from user where authentication_string is not null; #查询密码是null的账号密码信息
select user,authentication_string from user where authentication_string <=>null; #安全等于,可以判断null值,即为null
*********************************************************************************************************
三、排序查询
语法:select 查询列表 from 表 [where 筛选条件] order by 排序列表 [asc|desc],asc升序,desc降序,如果不写默认为升序。
select * from biao_name order by ID asc;
select * from biao_name where ID<3 order by ID asc;
【计算长度】:length()
select length(name) 名字长度,name,ID,phone_num from biao_name order by 名字长度 desc;
【按多个字段排序】
select length(name) 名字长度,name,ID,phone_num from biao_name order by 名字长度 desc,ID asc;#名字长度字段从高到低,一样长度的从低到高