# MySQL 增删改查

# 创建表

CREATE TABLE class(
  id          int           primary key auto_increment, 
  cname       varchar(30)   not null, 
  description varchar(100)  null
);
DESC class;
DROP TABLE IF EXISTS class;
CREATE TABLE tests LIKE class;
DESC tests;

# 插入数据项

INSERT class SET cname = "php" , description = "学习php 开发网站";
INSERT INTO class (cname, description) 
VALUES 
("linux","服务器知识"),
("mysql","数据库知识");

# 查询基本操作

# 获取所有行

SELECT * 
FROM class;
SELECT name, id AS ids 
FROM class;

# 条件查询

SELECT *
FROM class
WHERE 
description LIKE '%p%';
SELECT * 
FROM class 
WHERE 
  description LIKE '%p%'
  OR 
  id > 2;

案例

select concat(cname, description)  as class_info from class;
create TABLE stu(
  id int primary key auto_increment,
  sname char(10),
  class_id int default null,
  age smallint not null
);
INSERT 
INTO stu (sname, class_id, age) 
VALUES
('小赵',1,33),
('小钱',1,33),
('小孙',1,33),
('小李',null,33);
select * from stu
where class_id=2
or sname like '%李%';

# 去重

SELECT DISTINCT class_id FROM stu;

# 范围查询

SELECT * FROM stu WHERE age>=20 AND age<=40;
SELECT * FROM stu WHERE BETWEEN 20 AND 40;
SELECT * FROM