选择数据库

USE yuisql

回显信息:

USE yuisql
> OK
> 时间: 0s

SELECT语句

SELECT语法

SELECT * FROM table_name;
SELECT column_name,column_name FROM table_name;

例如:

#从数据库中选取数据,选取结果会存在一个表中
SELECT * FROM person

SELECT

SELECT name,country FROM person

SELECT

SELECT DISTINCT语句

SELECT DISTINCT column_name FROM table_name;

DISTINCT用于去掉相同的值显示不同的值,例如:

SELECT DISTINCT country FROM person;

SELECT

WHERE语法

SELECT column_name,column_name FROM table_name WHERE column_name operator value;

举例

SELECT id,name FROM person WHERE country='US';

WHERE

INSERT语句

INSERT INTO table_name (column1,column2,column3...) VALUES (value1,value2,value3...);

举例

INSERT INTO person (id,name,age,country) VALUES (6,'Jack',22,'FR');

回显信息:

INSERT INTO person (id,name,age,country) VALUES (6,'Jack',22,'FR')
> Affected rows: 1
> 时间: 0.009s

添加成功

UPDATE语句

UPDATE table_name SET column1=value1,column2=value2,... WHERE column=value;
#不可以省略WHERE,不然的话全部都会被修改 

举例

UPDATE person SET age=25,country='UK' WHERE id=6;

回显信息:

UPDATE person SET age=25,country='UK' WHERE id=6
> Affected rows: 1
> 时间: 0.011s

UPDATE

DELETE语句

DELETE FROM table_name WHERE column=value;

举例

DELETE FROM person WHERE name='Jack';

删除成功

删除所有数据

DELETE * FROM person;

AND和OR运算符

AND查询

SELECT * FROM person WHERE country='US' AND age>20;

AND查询结果

OR查询

SELECT * FROM person WHERE country='US' OR age>20;

OR查询结果

AND和OR并用

SELECT * FROM person WHERE age>14 AND (country='US' OR country='UK');

AND和OR并用

ORDER BY关键词

查找结果升序排序

SELECT * FROM person ORDER BY age;

ORDER BY升序排序

查找结果降序排序

SELECT * FROM person ORDER BY age DESC;

ORDER BY降序排序

按照指定的两列排序

SELECT * FROM person ORDER BY country,age;

指定两列排序