測試環境:mysql 5.5.57-log
如果沒有特別說明,語句在mysql中測試通過。
一、基礎
說明 | 語句 | 注 |
---|---|---|
查看所有數據庫 |
show databases; | |
操作數據庫 |
use database_name; | |
查看所有表 | show tables; | |
創建數據庫 | create database database_name; | |
刪除數據庫 | drop database database_name; | |
創建新表 | create table table_name(col1 type [not null] [primary key],col2 type [not null]); | |
根據已有表創建新表 | create table table_new like table_old; | 使用已有表格式創建新表,複製結構(包括主鍵,約束等)但不復制數據 |
create table table_new as select statemet; | 根據已有表數據創建新表,複製數據但不復制結構(主鍵,約束等) | |
刪除表 | drop table table_name; | |
創建視圖 | create view view_name as select statemet; |
視圖數據修改也會導致表數據修改 |
刪除視圖 | drop view view_name; | |
查看錶結構 | desc table_name; | |
添加主鍵 | alter table table_name add primary key(col); | |
刪除主鍵 | alter table table_name drop primary key; | 如果主鍵具有自增約束,則應先刪除自增約束再執行刪除主鍵操作 |
創建索引 | create [unique] index index_name on table_name(col) ; | 索引是不可以更改的,必須刪除索引重新再建 |
刪除索引 | drop index index_name on table_name; | |
插入數據(insert into) | insert into table_name(col1,col2...) values(value1,value2...); | |
刪除數據(delete from) | delete from table_name where... | |
更新數據(update..set..) | update table_name set field1=value1,field2=value2... where... | |
選擇數據(select) | select * from table_name where... | |
排序(order by) | select * from table_name order by col1,col2 [desc] | order by 默認asc升序,如果需要降序則要在最後加上desc |
模糊查找(like) | select * from table_name where field like '%value%'; | |
數據量查詢(count) | select count(*) from table_name where... | |
指定數據量查詢(limit) | select * from table_name limit m,n; | limit m,n 其中m指記錄開始的index,從0開始,表示第一條記錄。n是指一共取n條數據 |
求和(sum) | select sum(col) from table_name... | |
平均(avg) | select avg(col) from table_name... | |
最大(max) | select max(col) from table_name... | |
最小(min) | select min(col) from table_name... | |
分組(group by) | select * from table_name group by col; | 用於統計信息,sum,avg,max,min是分組的標準,如sum(id) 則col爲id |
並運算(union) | select * from table1 union select * from table2; | union運算符通過組合兩個表所有行,並消除所有重複,生成結果表 |
select * from table1 union all select * from table2; | union all運算符不消除重複 | |
差運算(except) (mysql不支持) |
select * from table1 except select * from table2; | except運算符通過包括table1中存在而table2中不存在的行,並消除所有重複(mysql中用not in) |
select * from table1 except all select * from table2; | except all運算符不消除重複行 | |
交運算(intersect) (mysql不支持) |
select * from table1 intersect select * from table2; | intersect運算符通過只包括兩表中都有的行,並消除所有重複(mysql中用exists) |
select * from table1 intersect all select * from table2; | intersect all運算符不消除重複 | |
左外連接(left join on) | select a.x,b.y from a left join b on a.id=b.id; | left join...on結果既包括左連接表a的所有行,也包括在b表中的匹配行 |
右外連接(right join on) | select a.x,b.x from a right join b on a.id=b.id; | right join...on結果既包括右連接表b的所有行,也包括在a表中的匹配行 |
全外連接(full join) | select * from a full join b; | 結果集行數是表a與表bhang shaxb |
二、進階
說明 | 語句 | 注 |
---|---|---|
拷貝表數據 | insert into b(field1,field2...) select f1,f2... from a; | 表b已經存在 |
在線視圖查詢 | select * from (select field1,field2 from tab) as t... | |
between...and... | select * from tab where field between value1 and value2; | between限制範圍時包括了邊界,即包括了value1與value2 |
select * from tab where field not between value1 and value2; | not between獲得的結果集不包括邊界 | |
in | select * from tab where field in(value1,value2); | in |
select * from tab where field not in(value1,value2); | not in | |
子查詢 | select * from tab1 where field in(select * from tab2 where...); | |
having子句 | select * from tab group by field having count(field)>1 ;(獲取重複數據) | having通常與group by一起使用,用於根據指定條件過濾分組。單獨使用則和where用法一樣 |
exists | select * from tab1 where exists(select * from tab2 where tab1.field=tab2.field); | 該語句返回兩表中都有的數據。exists並不會返回任何行數據,只會返回True或者False。 |
select * from tab1 where not exists(select * from tab2 where tab1.field=tab2.field); | 返回tab1中有而tab2中沒有的數據 | |
前10條記錄(top) (mysql不支持) |
select top 10 * from tab where... | (mysql中用limit) |
隨機數生成(newid()) (mysql不支持) |
select * from tab order by newid(); | 對查詢結果隨機排序(mysql中用rand()) |
去重複(distinct) | select distinct * from tab | 獲取去掉重複後的數據 |
1,create table temp as select distinct * from tab; |
三步解決mysql數據表中刪除重複數據,這種方法涉及數據移動,不適合大量的數據操作 | |
2,delete from tab; | ||
3,insert into tab selete * from temp; | ||
留待更新.............. | ||