Linux学习-SQL之SQL语句
Posted 丢爸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-SQL之SQL语句相关的知识,希望对你有一定的参考价值。
DML(Data Mainpulate Language):
DELETE
INSERT INTO
UPDATE
需先进行查询后,方可进行以上操作
INSERT INTO tb_name(col1,col2…,)values(val1,val2,…)[,(val1,val2,…),…]
字符型:必须使用单引号
数值型:不需要引号
日期时间型:不需要引号
空值:NULL
REPLACE INTO 使用方法同INSERT INTO,出现重复数据则直接替换数据
DELETE FROM tb_name where condition [order by][limit ];
TRUNCATE tb_name 清空表,并重置计数器(auto_increment)
UPDATE tb_name SET col1=…,col2=… WHERE …
#插入数据
mysql> insert into tutors set Tname='Tom',Gender='F',Age=30;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | M | 49 |
| 10 | Tom | F | 30 |
+-----+--------------+--------+------+
10 rows in set (0.00 sec)
#获取最后一条记录
mysql> select * from tutors order by TID desc limit 1;
+-----+-------+--------+------+
| TID | Tname | Gender | Age |
+-----+-------+--------+------+
| 10 | Tom | F | 30 |
+-----+-------+--------+------+
1 row in set (0.00 sec)
#获取最后的索引
mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 10 |
+------------------+
1 row in set (0.00 sec)
#将学生表中年龄大于20的学生插入到tutors表中
mysql> select Name,Gender,Age from student where Age >20;
+-------------+--------+------+
| Name | Gender | Age |
+-------------+--------+------+
| DingDian | M | 25 |
| HuFei | M | 31 |
| Xuzhu | M | 26 |
| LingHuchong | M | 22 |
+-------------+--------+------+
4 rows in set (0.00 sec)
mysql> insert into tutors (Tname,Gender,Age) select Name,Gender,Age from student where Age >20;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | M | 49 |
| 10 | Tom | F | 30 |
| 11 | DingDian | M | 25 |
| 12 | HuFei | M | 31 |
| 13 | Xuzhu | M | 26 |
| 14 | LingHuchong | M | 22 |
+-----+--------------+--------+------+
14 rows in set (0.00 sec)
以上是关于Linux学习-SQL之SQL语句的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取