4-1插入记录INSERT
INSERT [INTO] tbl_name [(col_name)] {VALUES|VALUE} ({expr|DEFAULT},...), (...), ... #可以一次性插入多条记录
INSERT [INTO] tbl_name SET col_name = {expr | DEFAULT}, ... #可以使用子查询
INSERT [INTO] tbl_name [(col_name,...)] SELECT ... #可以将查询的结果插入到指定的表中
4-2更新记录UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] tbl_reference SET col_name1={expr1 | DEFAULT} [,col_name2={expr2|DEFAULT}] ... [WHERE where_condition] #单表更新
4-3-1删除记录DELETE
DELETE FROM tbl_name [WHERE where_condition] #单表删除
4-3-2多表删除
从数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id
或
DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
2、从数据表t1里在数据表t2里没有匹配的记录查找出来并删除掉
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
或
DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
3、从两个表中找出相同记录的数据并把两个表中的数据都删除掉
DELETE t1,t2 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25
注意此处的delete t1,t2 from 中的t1,t2不能是别名
#delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(MYSQL 版本不小于5.0在5.0中是可以的)
上述语句改写成
#delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(MYSQL 版本小于5.0在5.0中是可以的)
附:上述语句运行的环境 MYSQL 4.0.2以上
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id
或
DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
2、从数据表t1里在数据表t2里没有匹配的记录查找出来并删除掉
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
或
DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
3、从两个表中找出相同记录的数据并把两个表中的数据都删除掉
DELETE t1,t2 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25
注意此处的delete t1,t2 from 中的t1,t2不能是别名
#delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(MYSQL 版本不小于5.0在5.0中是可以的)
上述语句改写成
#delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(MYSQL 版本小于5.0在5.0中是可以的)
附:上述语句运行的环境 MYSQL 4.0.2以上
4-4查找记录SELECT
SELECT select_expr [,select_expr ...]
[
FROM table_reference
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC|DESC],....] #分组,可以指定列名和列位置
[HAVING where_condition] #对某一部分分组,保证分组条件要么聚合函数(MAX,SUM),或该语句出现在select语句中
[ORDER BY {col_name | expr | position} [ASC|DESC] ,...] #多用于升降序
[LIMIT {[offset,] row_count | row_count OFFSET offset}] #限制查询结果返回数量
]
5-1子查询
子查询(Subquery)是指出现在其他SQL语句中的SELECT字句。
#SELECT * FROM t1 #outer query/outer statement 外查询
WHERE
col1 = (SELECT col2 FROM t2); #subquery 子查询
子查询是嵌套在查询内部,且必须始终出现在圆括号内,可以包含多个关键字或条件,如 DISTINCT、GROUP BY 、ORDER BY 、LIMIT,函数等。 子查询外层查询可以是:SELECT,INSERT,UPDATE,SET或DO。
子查询可以返回标量、一行、一列或子查询。
使用比较运算符的子查询
包括=/>/</>=/<=/<>/!=/<=>
语法结构: operand comparison_operator subquery
#SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price >=(SELECT ROUND (AVG(goods_price),2) FROM tdb_goods);
子查询结果大于一个结果是需要使用ANY /SOME/ALL 来修饰
operand comparison_operator ANY (subquery)
operand comparison_operator SOME (subquery)
operand comparison_operator ALL (subquery)
运算符/关键词 | ANY |
SOME
|
ALL
|
>/>= |
最小值 |
最小值
|
最大值
|
</<= | 最大值 |
最大值
|
最小值
|
=
|
任意值
|
任意值
|
|
<>/!= |
|
|
任意值 |
语法结构:
operand comparison_operator [NOT] IN (subquery) =ANY 运算符与IN等效。
!=ALL或<>ALL运算符与NOT IN等效。
使用[NOT] EXISTS 的子查询
如果子查询返回任何行,EXISTS将返回TRUE;否则将返回FALSE。
5-2 使用INSERT....SELECT将查询结果写入数据表
语法结构:
INSERT [INTO] tbl_name [(col_name,...)] SELECT ...
#->INSERT tbl_goods_cates(cate_name) #插入商品品牌名
->SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; #从商品表中查找品牌名
5-3 多表更新
语法结构:
UPDATE table_references SET col_name1 = {expr1 | DEFAULT} [,col_name2 = {expr2 | DEFAULT}]... [WHERE where_condition]
#->UPDATE tdb_goods INNER JOIN tbd_goods_cates #使用内连接更新
->ON goods_cate = cate_name #条件为 表1字段=表2字段
-> SET goods_cate = cate_id; #设置表1字段 名称= 表2 字段ID
5-4 多表更新一步到位
创建数据表同时将查询结果写入到数据表
语法结构:
CREATE TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] select_statement
#->CREATE TABLE tdb_goods_brands #创建商品类型表
->(
->brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, #创建类型表ID字段
->brand_name VACHAR(40) NOT NULL #创建类型名字段, 长度40 ,非空
->)
->SELECT brand_name FROM tdb_goods GROUP BY brand_name; #从商品表中查找类型
5-5 连接
表的参照关系:
table_references #表1
{[INNER | CROSS] JOIN | {LEFE | RIGHT} [OUTER] JOIN} #连接类型
table_references #表2
ON conditional_expr #连接条件
连接类型
INNER JOIN ,内连接 # 在MySQL中,JOIN,CROSS JOIN 和INNER JOIN是等价的,A∩B
LEFT [OUTER] JOIN,左外连接 # AU(A∩B)
RIGHT [OUTER] JOIN , 右外连接 #(A∩B)UB
多表连接#->SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g #使用别名,查找
->INNER JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_ID #条件1
->INNER JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id; #条件2
无限分类表的设计(通过自身连接实现 存在三个字段 ID,name,父类ID)
#->SELECT s.type_id,s.type_name,p.type_name #查询
->FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS p #左外连接 FROM子查询中必须使用别名
->ON s.parent_id = p.type_id; #连接条件
多表删除
语法结构:DELETE tbl_name[.*] [,tbl_name[.*]] ... FROM table_references [WHERE where_condition]
#->DELETE t1 FROM tdb_goods AS t1 #删除表t1表
->LEFT JOIN #使用左外连接
-> (SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2) AS t2 #子查询 商品名称类型数量大于2
->ON t1.goods_name= t2.goods_name #连接条件
->WHERE t1.goods_id > t2.goods_id; #删除条件为id更大的