數據庫基本操作命令大全--(一)

select [distinct] [ * ] [列名1,列名2] from 表名 [where 條件]
distinct : 去除重複的數據
select distinct sex from student;      --  男/女
--商品分類:手機數碼,鞋靴箱包
1.分類的ID
2.分類名稱
3.分類描述

create table category(
	cid int primary key auto_increment,
    cname varchar(10),
    cdesc varchar(31)
);

insert into category values(null,'手機數碼','電子產品,黑馬生產');
insert into category values(null,'鞋靴箱包','江南皮革廠');
insert into category values(null,'香菸酒水','江南皮革廠');
insert into category values(null,'酸奶餅乾','江南皮革廠');
insert into category values(null,'饞嘴零食','瓜子花生');

--所有商品
1.商品id
2.商品名稱
3.商品的價格
4.生產日期
5.商品分類的id

商品和商品的分類:所屬關係
create table product(
	pid int primart key auto_increment,
    pname varchar(10),
    price double,
    pdate timestamp,
    con int
);

insert into product values(null,'小米mix4',998,null,1);
insert into product values(null,'錘子',2888,null,1);
insert into product values(null,'阿迪',99,null,2);
insert into product values(null,'老村長',88,null,3);
insert into product values(null,'勁酒',35,null,3);
insert into product values(null,'安慕希',78,null,4);
insert into product values(null,'衛龍',1,null,5);
insert into product values(null,'小熊餅乾'null,5);
insert into product values(null,'三隻松鼠',132,null,5);



--簡單查詢
--查詢所有的商品:
select * from product;


--查詢商品名稱和商品價格

select pname,price from product;

--別名查詢,as 關鍵字,as關鍵字是可以省略的
	--表別名:selece p.pname,p.price from product p;(主要用在多表查詢);
	
	--列別名:select pname as 商品名稱,price as 商品價格 from product;
	select pname as 商品名稱,price as 商品價格 from product;
    省略關鍵字
    select pname 商品名稱,price 商品價格 from product;
--去 掉重複的值
	--查詢商品所有的價格
	select prict from product;
	select distinct price product;
--select運算查詢:僅僅在查詢結果上做了運算 + - * /
	select *,price*1.5 from product;
	select *,price*1.5 as 折後價 from product;
	
	select *,price*0.9 from product;
	
--條件查詢(where關鍵字)
	指定條件,確定要操作的記錄
--查詢商品價格>60元的所有商品信息
	select * from product where price > 60;
	
--where 後的條件寫法
	--關係運算符: > >= < <= = != <>
	<> :不等於 : 標準SQL語法
	!= :不等於:  非標準SQL語法
	--查詢商品價格不等於88的所有商品
	select * from product where price <> 88;
	
	--查詢商品價格在10到100 之間
	select * from product where  price > 10 and price < 100;
	
	between...and...
	select * from product where price between 10 and 100;
	--邏輯運算: and , or , out
	
	--查詢出商品價格小於35 或者商品價格大於900
	select * from product where price < 35 or price > 900;
	
	--like : 模糊查詢
	_:代表的是一個字符
	
	%:代表的是多個字符
	--查詢出名字中帶有餅的所有商品
	select * from product where pname like '%餅%';
	--查詢第二個名字是熊的所有商品
	select * from product where pname like '_熊%';
	
	--in 在某個範圍中獲得值
		--查詢出商品分類ID在1,4,5裏面的所有商品
		select * from product where cno in (1,4,5);                                              --查詢出商品分類ID不在 3 裏面的所有商品
         select * from product where con not in (3);
--排序查詢:order by 關鍵字

	asc: ascend 升序(默認的)
	desc: descend 降序
	
	--0.查詢所有商品,按照價格進行排序
	select * from product order by  price;
	--1.查詢所有的商品,按價格進行降序排序(asc-升序  desc-降序);
	select * from product order by  price desc;
	--2.查詢名稱有  小 ,按價格降序排序
     select * from product where pname like '%小%';
     select * from product where pname like '%小%' order by price asc;
	
     --組合排序  :先按第一個字段進行排序,如果第一個字段相同,才按第二個字段進行排序,
     --查詢價格升序,如果價格相同,按分組(con)降序排序
      select * from product order by price asc, con desc;
--聚合函數:
     sum()  求和
     avg()  求平均值
     count():統計數量
     max:最大值
     min():最小值              
     --1.獲得所有商品價格的總和:
     select sum(price) from product; 
     --2.獲得所有商品的平平均價格
     select avg(price) from product;               
     --3.獲得所有的商品的個數,值爲NULL跳過。
     select count(*) from product;  
     --4.獲得所有商品的個數,如果值爲 NULL 時也計入統計。
     select count(ifnull(price,0)) from product;     
                   
	--注意:where 條件後面不能接聚合函數
     select * from product where price > avg(price);  
     --查出商品價格大於平均價格的所有商品
     查出所有商品
     select * from product;
     大於
  	 平均價格
     select avg(price) from product;
                   
     select * from product where price > avg(price);
     轉換後的格式爲: -- 
     select * from product where price >( select avg(price) from product ); 
     
                   
--分組:group by
     --1.根據con字段分組,分組後統計商品個數
         select cno,count(*)
          from product grop by cno;
     --2.根據con分組,分組統計每組商品的平均價格 , 並且商品平均價格 > 60
         select  avg(price)
     	 from product group by cno
         having avg(price) > 60;
     --3.根據con分組,分組統計每組商品的個數,並且商品的價格要大於50;
         select count(pid),con from product where price > 50 group by con;          
                   
     --##having關鍵字可以接聚合函數的  出現在分組之後
        -- having與where的區別
                having是在分組後對數據進行過濾.
                where是在分組前對數據進行過濾
                having後面可以使用聚合函數
                where後面不可以使用聚合函數
--限制:limit語句   LIMIT 是 限制 的意思,所以 LIMIT 的作用就是限制查詢記錄的條數。
        --1.查詢所有商品,從第三條開始顯示,顯示六條(如果不夠六條,有多少顯示多少。如果第一個參數是0可以簡寫)
              	select * from product limit 2,6;
                   
--編寫順序               
-- s..f..w..G..H..o
   select .. from .. where .. group by .. having .. order by           

--執行順序
   F..W..G..H..S..O
   from .. where ..group by .. having .. select .. order by               
   得到這個表 .. 條件判斷 .. 還是這個表再進行分組 .. 再做一個條件篩選 .. 最後進行控制顯示 .. 顯示的結果再去排序            
        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章