語法總結 :
順序在最後
" * " 就至關於 一整排的意思
insert into 是寫入這行 若是遇到非空 必須賦值 哪怕是 null
alter 是改變的意思 用於結構的增長列 通常和 alter table.... add ...
update 是更新數據的意思 用於給 字段 更改值 update ....set....
想顯示兩個 就在 select後面 兩個字段而且用個 , 隔開 如 : pid,pname
想看一組的數據 就先引用 alter add 加上cid列 再 用update set給列分開 最後 用group by 查這組的值
4.1 數據準備
#
建立商品表
create table product(
pid int primary key auto_increment,
pname varchar(20),
price double,
pdate timestamp
)
#
自動增加列:auto_increment,要求:1,必須整型(int) 2.必須是主鍵
insert into product values (null,'
譚妮平',0.01,null);
insert into product values (null,'
李士雪',38,null);
insert into product values (null,'
左慈',-998,null);
insert into product values (null,'
黃迎',99999,null);
insert into product values (null,'
南國強',99998,null);
insert into product values (null,'
士兵',1,null);
insert into product values (null,'
李士兵',698,null);
4.2 簡單查詢(重點前3個)
查詢的語法:
s
elect 要查詢的字段 from 表名
where
條件;
1.
查詢
全部
商品
s
elect
* from
表名
;
select * from product;
2.
查詢商品名和商品價格
s
elect 要查詢的字段(多個字段用逗號隔開) from 表名;
select
pname,price
from product;
3.
查詢全部商品信息使用表別名
【單表沒啥卵用,主要用於多表操做!】
s
elect
* from
表名 別名;
select * from product p;
4.
查詢商品名,使用列別名(相對錶別名使用要少一些。)
select
列名 列別名
from
表名;
4.3 條件查詢
1.
查詢商品名稱爲"左慈"的商品信息 - --- 查一行
s
elect
* from product where pname=’
左慈
’
;
2.
查詢價格>60元的全部商品信息
3.
查詢商品名稱含有"士"字的商品信息【
模糊查詢
】
select * from
表名
where
字段名
like ‘%
士%
’
;
【%表示任意個字符】
查詢商品名稱以"士"字開頭的商品信息
select * from
表名
where
字段名
like ‘
士%
’;
【%表示任意個字符】
查詢商品名稱第二字爲"士"字的商品信息
select * from
表名
where
字段名
like ‘_
士%
’;
【_表示1個字符】
4.
查詢商品id在(3,6,9)範圍內的全部商品信息【關鍵字
in
】
s
elect
* from product where pid
in(3,6,9);
and or not
5.
查詢商品名稱爲士兵
而且
價格>0的商品信息
說明:而且(
and
)
6.
查詢商品名稱含有士字
或者
價格>100的商品信息
7.查詢
id
不是2
的商品信息 -----
not (pid = 2); != 也行
8.
查詢價格在100到10000之間的商品信息
between A and B (先後閉區間 都包)
4.4 排序
排序語法:select * from 表名
order by
指定的
字段名稱
[
asc
升序]/desc降序
-
查詢全部的商品,按價格進行排序(升序、降序)
升序:
select * from product order by price asc;
或者
select * from product order by price;
降序:
select * from product order by price asc;
1.2.
查詢名稱有"士"的商品信息而且按照價格降序排序
select * from product where pname like '%
士%' order by price desc;
注意:order by排序操做必須放在where 條件的後面!!!
4.5 聚合
以前的查詢操做都是橫向操做(以行爲單位進行查詢),使用聚合函數,能夠進行縱向操做(以列爲單位進行查詢,查詢的結果是單獨的一列) 就和Excel同樣
語法:select 函數名稱(給定列名) from 表名
2.1.
得到全部商品的價格的總和 sum
s
elect
sum(price) from product;
2.2.
得到全部商品的平均價格 avg
se
lect
avg(price) from product;
2.3.
得到全部商品的個數 count
s
elect
count(
*
) from product;
4.6 分組
語法:select * from 表名
group by
分組的字段名稱 這個只在把一組當成一個總體的時候才用,分組是用的加列的思想;
1.
添加分類id (alter table product add cid varchar(32);) --加上cid這個列而已
2.
初始化數據 --- 給這個列賦值
update product set cid='1';
update product set cid='2' where pid in (5,6,7);
查詢:
3.1.
根據cid字段分組,分組後統計商品的個數。 --- 統計每組的
select cid,count(cid) from product
group by
cid;
3.2.
根據cid分組,分組統計每組商品的平均價格,而且平均價格大於20000元。
select cid,avg(price) from product
group by
cid having avg(price)>20000;
若是進行了分組操做,還帶有條件,不能使用where關鍵字,必須使用having,並且只能放在最後面!!!
4.7 分頁查詢
LIMIT
select * from product limit 2; 查前兩個
select pname from product limit 4,2;
公式 :
(要看的頁碼數 - 1)*每頁顯示大小
4.9 刪除 delete from 表名 全刪
delete from 表名 where 條件
delete from product where pid=8;
4.8單表查詢總結 順序
開發中關於查詢語句應該怎麼去寫
?
【關鍵字出現的順序以下:】
select
被查詢的字段(全部的字段寫
*
)【通常都是 的 字後面的內容】
from
表名
where
條件 【通常都是 的 字前面的內容】
grou
p by
分組的字段名稱 【通常會明確指定根據什麼來分組】
order
by
升序仍是降序(
asc/desc
)
【通常會明確指定根據什麼來分組】
having 條件(分組後的條件)
【通常會明確指定根據什麼來分組】