mysql的学习笔记(四)

---恢复内容开始---

1.插入操作 INSERT

(1)INSERT table_name(col_name,...) VALUES(...),(....)

CREATE TABLE  user(
 id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password VARCHAR(32) NOT NULL,
age TINYINT UNSIGNED NOT DEFAULT 10,
sex BOOLEAN
);

为表插入数据

INSERT user VALUE(NULL,'JACK','123',25,1);

(2)可使用子查询插入

INSERT user(username) SELECT username FROM user WHERE age>30 
INSERT user SET username='Den',password='123';

(3)INSERT table_name SELECT...

2.更新记录(单表更新) UPDATE

UPDATE table_name SET col_name=值 WHERE ID=..

3.删除记录(单表删除) DELETE

DELETE FROM table_name WHERE ...

4.查找记录 SELECT

SELECT * FROM table_name

(1).为字段取别名

SELECT username AS name FROM user AS u;

(2).WHRER对子路进行过滤,没有WHREE自居,则显示所有记录。在WHERE表达式,支持函数或运算符

SELECT username FROM user WHERE id = 3

(3).对查询的结果分组   GROUP BY

SELECT * FROM user GROUP BY sex;

(4).having语句设置分组条件,分组的条件如果不是聚合函数,必须出现在SELECT的语句当中。

SELECT * FROM user GROUP BY sex HAVING age>15

(5).对查询的结果进行排序 ASC升序,DESC倒叙

SELECT * FROM user ORDER BY id DESC

(6).limit限制查询结果返回数量  分页公式(当前页码-1) *每页显示的记录数

SELECT * FROM user limit 0,2

---恢复内容结束---

上一篇:Delphi- DLL操作


下一篇:HDU 1038 - Biker's Trip Odometer