MySQL数据库基础

进入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');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弈-剑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值