標籤:
建立資料庫: create database 庫名;
建立表: create table 表名 (列名 資料類型(長度) primary key,列名 資料類型 是否為空白…… );
注意:最後一列不要加逗號。多條語句一起執行,加分號。代碼符號半形。
建立表的關鍵字:主鍵 primary
非空 not null
自增長列 auto_increment
外鍵關係 foreign key(列名)references 表名(列名)
添加資料: insert into 表名 values(‘’,‘’,‘’) 括弧內的值得個數和表的列數相同
insert into 表名 (列名,列名) values(‘’,‘’) 添加指定的列
修改資料: update 表名 set 列=新值 where 條件;
刪除資料: delete from 表 where+條件;
普通查詢: select (結果顯示列) from 表名
條件查詢: select (結果顯示列) from 表名 where 條件
排序查詢: selert (結果顯示列) from 表名 order by 列名 (asc/desc),列名 (asc/desc)......
彙總函式: select count(*)from 表名 #取個數
select sum(列名) from 表名 #查詢列的和
select avg(列名) from 表名 #查詢平均值
select max(列名) from 表名 #查詢最大值
select min(列名) from 表名 #查詢最小值
分頁查詢: select * from 表名 limit n,m #跳過n條取m條
分組查詢: select 列名 from 表名 group by 列名 #簡單的分組查詢
select brand from car group by brand having count(*)>2 #查詢系列裡面車的數量大於2的系列
去重查詢: select distinct 列名 from 表名
修改列名: select 列名 as ‘新列名’ from 表名
模糊查詢: select * from 表名 where name like ‘_奧迪%’ #任意組合 %代表無限個字元 _代表一個字元 任意組合
離散查詢: select * from 表名 where 列名 in/not in (‘’,‘’,‘’)
大於所有資料的:select * from 表名 where 列名>all(50,40,30)
MySQL 簡單語句(2)