
查詢SQL變量 show variables
1.表字段的操作
1.語法:alter table 表名 執行動作;
2.添加字段(add)
alter table 表名 add 字段名 數據類型;(尾插)
alter table 表名 add 字段名 數據類型 first;(頭插)
alter table 表名 add 字段名 數據類型 after 字段名;(指定插入)
3.刪除字段(drop)
alter table 表名 drop 字段名;
4.修改數據類型(modify)
alter table 表名 modify 字段名 新數據類型;
5.重命名(rename)
alter table 表名 rename 表名;
2.字符類型
1.字符類型寬度與數值類型寬度的區別
1.數值類型寬度為顯示寬度,只用於select查詢顯示
占用儲存無關,可用zerofill查看效果
2.枚舉類型
1.單選(enum):字段名 enum(值1,值2...);
2.多選(set):字段名 set(值1,值2...);
(多項放在一個字符串內用,號隔開)
3.日期時間類型
1.date:“YYYY-MM-DD”
2.time:“HH:MM:SS”
3.datetime:“YYYY-MM-DD HH:MM:SS”
4.timestamp:“YYYY-MM-DD HH:MM:SS”
5.datetime:不給值默認返回Null
6.timestamp:不給值默認返回系統時間
3. 日期時間函數
1.now() 返回服務器當前的時間
2.curdate() 返回當前時期
3.curtime() 返回當前日期
4.year(date) 返回指定時間的年份
5.date(date) 返回指定時間的日期
6.time(date) 返回指定時間的時間
4.日期時間運算
1.語法格式
select * from 表名
where 字段名 運算符(時間 -interval 時間間隔單位);
時間間隔單位:
1 day | 2hour | 1 minute | 2year | month
5.表記錄管理
1.刪除表記錄
1.delete from 表名 where 條件;
注意:
如果不加where條件,所有記錄全部清空
2.更改表記錄
1.update 表名 set 字段1=值1,字段名2=值2,... where 條件
注意:
如果不加where條件,所有記錄全部更改
3.運算符操作
1.數值比較/字符比較
1.數值比較: = != > >= < <=
2.字符比較: = !=
2.邏輯比較
1.and
2.or
3.范圍內比較
1.where 字段名 between 值1 and 值2
2.where 字段名 in(值1,值2,....)
3.where 字段名 not in (值1,值2,...)
4.匹配空、非空
1.空:where name is null
2.非空:where name is not null
3.注意
1.NILL:空值,只能用is或is not取匹配
2.“ ” : 空字符串,用 = 或 != 去匹配
4.模糊比較
1.where 字段名 like 表達式
2.表達式
1._ : 匹配單個字符
2.% :匹配0到多個字符
NULL不會被統計
6.SQL查詢:
1語法順序:
3.select ... 聚合函數 from 表名
1.where
2.group by...
4.having ...
5.order by ...
6.limit ...;
2.order by
1.給出查詢結果進行排序
2...order by 字段名 升序/降序
升序:ASC(默認排序方式)
降序:DESC
3.limit(永遠放在SQL語句的最后)
1.作用:顯示查詢記錄的個數
2.用法
limit n 顯示n條記錄
limit m,n
m表示 從m+1條記錄開始顯示 顯示n條記錄
limit 2,3 顯示第3,4,5條記錄
3.分頁
每頁顯示5條記錄,顯示第4頁內容
第1頁:limit 0,5 #1,2,3,4,5
第2頁:limit 5,5
第3頁:limit 10,5
第4頁:limit 15,5
每頁顯示n條記錄,顯示第m頁:
limit(m-1)*n,n
4.聚合函數
avg(字段名):求該字段的平均值
sum(字段名):求和
max(字段名):最大值
min(字段名):最小值
count(字段名):統計該字段的個數
練習庫:
create database MOSHOU; use MOSHOU; create table hero( id int, name char(15), sex enum("男","女"), country char(10) )default charset=utf8; insert into hero values (1,"曹操","男","魏國"), (2,"小喬","女","吳國"), (3,"諸葛亮","男","蜀國"), (4,"貂蟬","女","東漢"), (5,"趙子龍","男","蜀國"), (6,"魏延","男","蜀國"); use MOSHOU; create table sanguo( id int, name char(20), gongji int, fangyu tinyint unsigned, sex enum("男","女"), country varchar(20) )default charset=utf8; insert into sanguo values (1,'諸葛亮',120,20,'男','蜀國'), (2,'司馬懿',119,25,'男','魏國'), (3,'關羽',188,60,'男','蜀國'), (4,'趙雲',200,66,'男','魏國'), (5,'孫權',110,20,'男','吳國'), (6,'貂蟬',666,10,'女','魏國'), (7,null,1000,99,'男','蜀國'), (8,'',1005,88,'女','蜀國');
練習
1、創建庫 studb2
2、在庫中創建表 t1 ,字段有3個:name、age、phnumber
3、查看表結構
4、在表中第一列添加一個 id 字段
5、把 phnumber 的數據類型改為 bigint
6、在表中最后一列添加一個字段 address
7、刪除表中的 age 字段
8、查看表結構
答案:
use studb2; create table t1( name char(20), age tinyint unsigned, phnumber char(11) ); desc t1; alter table t1 add id int first; alter table t1 modify phnumber bigint; alter table t1 add address varchar(50); alter table t1 drop age; desc t1;
練習
1、在表中插入3條記錄
2、查找2018年7月2日有哪些用戶充值了
3、查找2018年7月份充值的信息
4、查找7月30日10:00-12:00充值的信息
答案:
insert into t7 values (3,"小昭",19000520,3000,20180630000000), (4,"趙敏",19000521,4000,20180702000000), (5,"周芷若",19010522,3500,20180702100000); select * from t7 where date(shijian)="2018-07-02"; select * from t7 where date(shijian)>="2018-07-01" and date(shijian)<="2018-07-31"; select * from t7 where date(shijian)="2018-07-31" and time(shijian)>="10:00:00" and time(shijian)<="12:00:00";
練習
1、查詢1天以內的記錄
2、查詢1年以前的記錄
3、查詢1天以前,3天以內的記錄
答案:
select * from t7 where shijian > (now()-interval 1 day); select * from t7 where shijian < (now()-interval 1 year); select * from t7 where shijian < (now()-interval 1 day) and shijian > (now()-interval 3 day);
練習(表hero)
1、查找所有蜀國人的信息
2、查找所有女英雄的姓名、性別和國家
3、把id為2的記錄改為典韋,性別男,國家魏國
4、刪除所有蜀國英雄
5、把貂蟬的國籍改為魏國
6、刪除所有表記錄
答案:
select * from hero where country="蜀國"; select name,sex,country from hero where sex="女"; update hero set name="典韋",sex="男",country="魏國" where id=2; delete from hero where country="蜀國"; update hero set country="魏國" where name="貂蟬"; delete from hero;
練習
1、找出攻擊值高於200的蜀國英雄的名字、攻擊力
2、將吳國英雄中攻擊值為110的英雄的攻擊值改為100,防御力改為60
3、查找蜀國和魏國的英雄信息
答案:
select name as n,gongji as g from sanguo where gongji>200 and country="蜀國"; update sanguo set gongji=100,fangyu=60 where country="吳國" and gongji=110; select * from sanguo where country="蜀國" or country="魏國";
練習
1、查找攻擊值100-200的蜀國英雄信息
2、找到蜀國和吳國以外的國家的女英雄信息
3、找到id為1、3或5的蜀國英雄 和 貂蟬的信息
答案:
select * from sanguo where gongji between 100 and 200 and country="蜀國"; select * from sanguo where country not in("蜀國","吳國") and sex="女"; select * from sanguo where (id in(1,3,5) and country="蜀國") or name="貂蟬";
1、在蜀國英雄中,查找防御值倒數第二名至倒數第四名的英雄的記錄
2、在蜀國英雄中,查找攻擊值前3名且名字不為 NULL 的英雄的姓名、攻擊值和國家
答案:
select * from sanguo where country="蜀國" order by fangyu asc limit 1,3; select name,gongji,country from sanguo where country="蜀國" and name is not NULL order by gongji DESC limit 3;
1、攻擊力最強值是多少
2、統計id 、name 兩個字段分別有幾條記錄
## 空值 NULL 不會被統計,""會被統計
3、計算蜀國英雄的總攻擊力
4、統計蜀國英雄中攻擊值大於200的英雄的數量
答案:
select max(gongji) from MOSHOU.sanguo; select count(id),count(name) from sanguo; select sum(gongji) from MOSHOU.sanguo where country="蜀國"; select count(*) from MOSHOU.sanguo where gongji>200 and country="蜀國";
查詢變量 show variables1.表字段的操作 1.語法:alter table 表名 執行動作; 2.添加字段(add) alter table 表名 add 字段名 數據類型;(尾插) alter table 表名 add 字段名 數據類型 first;(頭插) alter table 表名 add 字段名 數據類型 after 字段名;(指定插入) 3.刪除字段(drop) alter table 表名 drop 字段名; 4.修改數據類型(modify) alter table 表名 modify 字段名 新數據類型; 5.重命名(rename) alter table 表名 rename 表名; 字符類型 1.字符類型寬度與數值類型寬度的區別 1.數值類型寬度為顯示寬度,只用於select查詢顯示 占用儲存無關,可用zerofill查看效果2.枚舉類型 1.單選(enum):字段名 enum(值1,值2...); 2.多選(set):字段名 set(值1,值2...); (多項放在一個字符串內用,號隔開) 3.日期時間類型 1.date:“YYYY-MM-DD” 2.time:“HH:MM:SS” 3.datetime:“YYYY-MM-DD HH:MM:SS” 4.timestamp:“YYYY-MM-DD HH:MM:SS” 5.datetime:不給值默認返回Null timestamp:不給值默認返回系統時間
3. 日期時間函數 1.now() 返回服務器當前的時間 2.curdate() 返回當前時期 3.curtime() 返回當前日期 4.year(date) 返回指定時間的年份 5.date(date) 返回指定時間的日期 6.time(date) 返回指定時間的時間4.日期時間運算 1.語法格式 select * from 表名 where 字段名 運算符(時間-interval 時間間隔單位); 時間間隔單位: 1 day | 2hour | 1 minute | 2year | month
5.表記錄管理 1.刪除表記錄 1.delete from 表名 where 條件; 注意: 如果不加where條件,所有記錄全部清空 2.更改表記錄 1.update 表名 set 字段1=值1,字段名2=值2,... where 條件 注意: 如果不加where條件,所有記錄全部更改 3.運算符操作 1.數值比較/字符比較 1.數值比較: = != > >= < <= 2.字符比較: = != 2.邏輯比較 1.and 2.or 3.范圍內比較 1.where 字段名between 值1 and 值2 2.where 字段名 in(值1,值2,....) 3.where 字段名 not in (值1,值2,...)
4.匹配空、非空 1.空:where name is null 2.非空:where name is not null 4.注意 1.NILL:空值,只能用is或is not取匹配 2.“” : 空字符串,用 = 或 != 去匹配 5.模糊比較 1.where 字段名 like 表達式 2.表達式 1._ : 匹配單個字符 2.% :匹配0到多個字符 NULL不會被統計5.SQL查詢: 3.select ... 聚合函數 from 表名 1.where 2.group by... 4.having ... 5.order by ... 6.limit ...; 2.order by 1.給出查詢結果進行排序 2...order by 字段名 升序/降序 升序:ASC(默認排序方式) 降序:DESC 3.limit(永遠放在SQL語句的最后) 1.作用:顯示顯示查詢記錄的個數 2.用法 limit n 顯示n條記錄 limit m,n m表示 從m+1條記錄開始顯示 顯示n條記錄 limit 2,3 顯示第3,4,5條記錄 4.分頁 每頁顯示5條記錄,顯示第4頁內容 第1頁:limit 0,5 #1,2,3,4,5 第2頁:limit 5,5 第3頁:limit 10,5 第4頁:limit 15,5 每頁顯示n條記錄,顯示第m頁: limit(m-1)*n,n4.集合函數 1.分類 avg(字段名):求該字段的平均值 sum(字段名):求和 max(字段名):最大值 min(字段名):最小值 count(字段名):統計該字段的個數