MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找。
将数据从T1表导入到T2表
1 | INSERT INTO T2 (C1,C2) SELECT C1,C2 FROM T1 [WHERE C1 = XX AND C2 = XX ORDER BY C1] |
使用T2表的NAME来更新T1表的NAME
1 | UPDATE T1 AS A, T2 AS B SET A.NAME = B.NAME WHERE A.TID = B.ID |
两表的关联更新
1 | UPDATE T_ROLE_USER AS A, |
自己和自己关联更新
1 | UPDATE T_DEPART AS A, |
两表关联删除,将删除两表中有关联ID并且T2表NAME为空的两表记录
1 | DELETE A,B FROM T1 AS A LEFT JOIN T2 AS B ON A.TID = B.ID WHERE B.NAME IS NULL |
将统计结果插入到表
1 | INSERT INTO SE_STAT_ORG ( |
三表关联更新
1 | UPDATE SE_STAT_USER A, |
带条件的关联更新
1 | UPDATE SE_STAT_USER A, |
加索引
1 | ALTER TABLE PROJECT ADD INDEX INDEX_USER_ID (USER_ID), |
删除列
1 | ALTER TABLE PROJECT DROP COLUMN PROJECT_STATUS, |
增加列
1 | ALTER TABLE PROJECT |
修改列,一般用MODIFY修改数据类型,CHANGE修改列名
1 | ALTER TABLE PROJECT CHANGE DICT_ID DICT_ID1 INT NOT NULL, |