insert into [表名] values (对应的列的数据);
注:插入的字段的数目和类型需要和表结构中要求的一致
insert into [表名] (若干个指定列) values (对应的列的数据);
根据需要只插入其中的某几列
insert into [表名] values (对应的列的数据) , (对应的列的数据)......;
select * from [表名];
注 : *是一个通配符,意思是把所有的列都查找出来.
select [列名] from [表名];
select [列名] as [别名] from [表名];
注: as可以省略.
select distinc [若干个列名] from [表名];
order by 指定针对哪个列进行排序
注: 默认为升序 . ASC 为升序(从小到大) DESC 为降序(从大到小)
select * from [表名] order by [列名] (asc 或 desc);
select name,chinese + math + english from exam_result order by chinese + math + english desc;
select name,chinese + math + english as total from exam_result order by total desc;
先把所有同学信息按照语文降序排序,再按照数学降序排序,再按照英语降序排序.
select * from exam_result order by chinese desc,math desc,english desc;
运算符 | 说明 |
---|---|
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL |
<=> | 等于, NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |
!=, <> | 不等于 |
BETWEEN a0 AND a1 | 范围匹配, [a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) |
IN (option, …) | 如果是 option 中的任意一个,返回 TRUE(1) |
IS NULL | 是 NULL |
IS NOT NULL | 不是 NULL |
LIKE | 模糊匹配。 % 表示任意多个( 包括 0 个)任意字符; _ 表示任意一个字符 |
算符 | 说明 |
---|---|
AND逻辑与 | 多个条件必须都为 TRUE(1),结果才是 TRUE(1) |
OR 逻辑或 | 任意一个条件为 TRUE(1), 结果为 TRUE(1) |
NOT逻辑取反 | 条件为 TRUE(1),结果为 FALSE(0) |
错误的写法 null = null
正确写法:
select * from exam_result where chinese <=> null;
select * from exam_result where chinese is null;
select * from exam_result where english < 60;
select * from exam_result where chinese > english ;
where 中不能使用别名
select name,chinese + math + english as total from exam_result where chinese + math + english > 200;
select * from exam_result where chinese > 80 and english > 80;
select * from exam_result where chinese > 80 or english > 80;
注: AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分
select * from exam_result where chinese between 80 and 90;
select * from exam_result where chinese >= 80 and chinese <= 90;
select * from exam_result where math in (45.0,60.0);
select * from exam_result where math = 45.0 or math = 60.0;
select * from exam_result where name like '孙%';
select * from exam_result where name like '孙_';
select * from exam_result where chinese like '9%';
-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
select name,chinese + math + english as total from exam_result order by total desc limit 3;
select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 3;
select name,chinese + math + english as total from exam_result order by total desc limit 10 offset 3;
select name,chinese + math + english as total from exam_result order by total desc limit 3 offset 100;
update [表名] set [列名] = [修改的值], [列名] = [修改的值] where 子句;
update exam_result set math = 80 where name = '孙悟空';
update exam_result set math = 50,chinese = 95 where name = '曹孟德';
mysql> update exam_result set chinese = chinese - 10 ;
mysql> update exam_result set math = math + 10 order by chinese + math + english asc limit 3;
delete from [表名] where [筛选条件];
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/wwzzzzzzzzzzzzz/article/details/122823870
内容来源于网络,如有侵权,请联系作者删除!