进入phpStudy2016自带的数据库(命令行)
在命令行中,上键可以复制上一行命令
#-u账户 root
#-p密码 root
mysql -u root -p
root
show databases; #查看有什么库
use test1; #进入test1数据库
select database(); #查看当前所在的数据库
show tables; #查看数据库有什么表
desc apple; #查看apple表
create database test2; #新建一个test2数据库
use test2;
create table pea(id int(8),name varchar(255)); #新建一个pea表
alter table pea add love varchar(255); #给pea表添加一个love字段
alter table pea drop love; #删除love字段
alter table pea change name username varchar(25); #将字段名name改为username,并且更改类型
drop database test2; #删除test2数据库
use test1;
insert into apple values(1,'apple1'); #插入数据(1,'qpple1')
#输出表qpple
select *
from apple;
insert into apple values(2,'apple2'),(3,'apple3');
insert into apple(name) value('apple4'); #单独插入name字段的数据
select id,name
from apple;
select *
from apple
where id='apple1';
MySQL具有兼容性
insert into apple values(3,'APPLE3');
select * from apple where name='apple3';
update apple set name='test' where name='APPLE3'; #修改数据
update apple set id=4,name='apple4' where name='test'; #修改多个字段的数据
update apple set name='test'; #如果没有限定条件,则会将所有的数据更改为'test'
delete from apple where id=4; #删除单个数据
delete from apple; #删除所有数据
select *
from apple
order by 1; #以第一个字段进行排序,默认为升序[asc]
select *
from apple
order by name; #以name字段进行排序
select *
from apple
order by id desc; #以字段id进行倒序排序
select *
from apple
limit 0,1; #从第1行开始,取1行
select *
from apple
limit 2,2; #从第3行开始,取2行
select group_concat(id) from apple; #多行数据一起输出
select group_concat(id,name) from apple;
select *
from apple
where name like %a%; #查询包含a的所有值
select *
from apple
where name like a%; #查询a开头的所有值
select *
from apple
where name like %a; #查询a结尾的所有值
select sleep(2); #休眠函数,服务器推迟2秒返回数据
运算符
+ - * /
and or not
select *
from apple
where id=3 and name='apple3';
select *
from apple
where id in (1,3);
select *
from apple
where id in (1,2,3);
select *
from apple
where id=1 and sleep(2); #当查询到符合条件的数据时,沉睡2s,不输出符合条件的数据
select *
from apple
where id=1 or sleep(2); #查询所有的数据都沉睡,并输出符合条件的数据
#将两张表上下连接
#union不输出重复值
#union all输出重复值
select * from apple
union
select * from pea;
select * from apple where id=1
union
select * from pea;
select * from apple
union all
select * from pea;
select 'orange' from apple; #不断输出orange
#union字段数必须相同,如果字段数不同,可以添加
select id,name,'NULL' from apple
union all
select * from banana; #假设banana有三个字段
#子查询
select *
from apple
where id in
(select id
from watermelons
where name='watermelons3');