Sql常用语句(MySql)

169 阅读1分钟

1、创建库

create schema `test`;

2、展示数据库

show databases;

3、切换数据库

use test;

4、展示表

show tables;

5、C-create 创建

insert into users (username, password, phone) values ('张三', '123', '13333333333');

6、R-read 查询

6.1 查询所有

select * from users;
或
select username, password from users;

6.2 条件查询

select * from users where username='张三';

--与
select * from users where username='张三' and password='123';

--或
select * from users where username='张三' or password='123';

--非
select * from users where username!='张三';
select * from users where username<>'张三';

--模糊查询
select * from users where username like '%张%';

--排序:默认倒叙desc
select * form users order by id desc;

7、U-update 更新

7.1 更新所有

update users set password='456';

7.2 带条件更新

update users set password='456' where username='张三';

8、D-delete 删除

8.1 删除表数据

delete from users where username='张三';

8.2 软删除

update users set state='0' where username='张三';
select * from users where state='1';