MySQL基礎命令操作

本文主要參考自:SQL簡明數據分析教程  及   MySQL---數據庫從入門走向大神系列(一)-基礎入門

 

一 創建與檢索

create database testbase character set utf8;  創建數據庫

use testbase;  使用該數據庫

 

create table mobile(

 num varchar(15) not null primary key,

 name varchar(15) not null,

 price int );    創建表

 

insert into mobile(num, name, price) values('1','p20','3500');  插入數據

insert into mobile values('2','m20','4000');

insert into mobile values('3','mg2','4500');

 

insert into tableA(columnA, columnB) select columnC, columnD from tableB;

插入檢索出的數據:將表BC D列數據複製到表A A B

 

select * from mobile;  檢索

 

 

select * from mobile order by name;  name排序

 

select distinct name,price from mobile order by num desc;  降序desc,升序asc

 

select distinct num from mobile limit 1,2;

1表示取第2行(從0起),2表示共取2

 

select * from mobile where num > '1' order by price desc limit 2;

num>1中降序排列的前2

 

select * from mobile where price between 4000 and 4500;

price大於等於4000且小於等於4500

或:select * from mobile where price>=4000 and price<=4500;

 

select * from mobile where price in (4000 ,4500);

price等於4000 4500 not in 指不等於

 

 

select * from phone where name like '%20';

模糊檢索,'%20':以20結尾的;  '20%':以20開頭的;  '%20%':含20的。

select * from phone where name like '_2_';

'_2_'3個字符,中間爲2的。

 

 

二 增刪與修改

alter table mobile rename phone;  改表名

alter table phone add column color varchar(10);  增加color列,column可以省略

alter table phone add column color varchar(10) after price;

after price表示加在price列後;用first表示加在第一列

 

alter table phone drop num;    刪除num

delete from phone where name='mg2';  刪除name列值爲mg2的行

 

alter table phone change column color colour varchar(10);  改列名colorcolour

 》》》》

 

update phone set colour='blue' where colour is null;

替換:修改顏色爲null的值爲bluenull前用is

update phone set price=4400,colour='red' where name='mg2';

 

create view testview as select * from phone where price >=4000;

  創建視圖(需表),只存於內存。

 

alter table mobile add id int primary key not null auto_increment first;

新增id列,實現自增。此處必須設爲primary key

或: alter table phone add id int first;

                select @rowid:=0;

                update phone set id=(@rowid:=@rowid+1);

  》》》》

 

 

三 函數

 

select name, price, price*quan as cost from phone;

namepriceprice*quan三列。可進行+, -, *, / 操作。

 

select concat

name ,   '(' ,   color ,   ')'  

 )  as prod from phone;   拼接字符串,例爲 加上括號

 

select name, upper(color) as colors from phone; 

color列的值轉成大寫。lower爲轉成小寫。

 

select * from phone where left(color,1)='b';

color列字符串中,左起第1個字符爲b的值。right爲右起。

 

按日期篩選,可用datemonthyear,日期格式必須爲yyyy-mm-dd

select name, price from phone

where Date(date) between '2018-05-01' and '2018-06-01';

  檢索出:

select name,date from phone where year(date)=2018;

 

select count(*) as num from phone;

計算表中行數(包括null)。如需忽略null,則改*爲列名

 

select sum(price) as cost from phone;

求和sum,平均avg,最大max,最小min,四捨五入round

括號裏面加distinct表示去掉重複值,如sumdistinct price

 

select name, price from phone

where price = (select max(distinct price) from phone );

 

 

 

存在判斷:

1. in()適合B表比A表數據小的情況

2. exists()適合B表比A表數據大的情況

3. 當A表與B表數據一樣大時,in與exists效率差不多,可任選一個使用。

 

select * from A
where id in(select id from B);

in()只執行一次,它查出B表中的所有id字段並緩存起來。之後,檢查A表的id是否與B表中的id相等,即逐個A數據遍歷B數據。所以B越小越好。

如:A表有10000條記錄,B表有100000000條記錄,那麼最多有可能遍歷10000*100000000

再如:A表有10000條記錄,B表有100條記錄,那麼最多有可能遍歷10000*100

 

select a.* from A a
where exists(select * from B b where a.id=b.id);

exists()會執行A.length次,它並不緩存exists()結果集,因爲exists()結果集的內容並不重要,重要的是結果集中是否有記錄,如果有則返回true,沒有則返回false,即二選一。

如:A表有10000條記錄,B表有100000000條記錄,那麼exists()執行10000次,因爲它只執行A.length

再如:A表有10000條記錄,B表有100條記錄,那麼exists()還是執行10000次。

 

舉例:

 

SELECT * FROM User

WHERE exists (SELECT * FROM Order WHERE user.id = order.user_id)

 

SELECT * FROM User

    WHERE id in (SELECT user_id FROM Order)

 

 

 

四 分組

select name,avg(price) as Price from phone group by name order by Price;

按不同的name分組,同一組中取平均值Price,再按Price升序排列

》》》

 

select name,avg(price) as Price from phone group by name having Price > 4000;

過濾功能:按不同的name分組,同一組取平均值Price,再取Price>4000的值

where比較:WHERE過濾行,而HAVING過濾分組

 

select * from phone where right(name,2)='20';

取符合name值右邊兩個字符爲20的數據。左邊用left

 

 

 

 

五 子查詢

常用的數據庫表都是關係表,若有如下3個表:

Orders表存儲訂單編號、客戶ID、訂單日期;

OrderItems表存儲各訂單內的具體物品;

Customers表存儲顧客的客戶ID、姓名。

 

現需要列出訂購物品A的所有顧客的姓名,實現步驟如下:

1. 檢索包含【1物品A】的所有【2訂單編號】;

2. 檢索前一步驟訂單編號的對應的【3客戶ID】;

3. 檢索前一步驟的客戶ID對應的【4客戶姓名】。

 

SELECT cust_name

FROM Customers

WHERE cust_id IN (SELECT cust_id

                     FROM Orders

                     WHERE order_num IN (SELECT order_num

                                            FROM OrderItems

                                            WHERE prod_id = 'A'  )  );

 

 

 

六 join與union連接

 

select * from Table A inner join Table B on Table A.id = Table B.id;

》》》》

 

 

select * from Table A left join Table B

on Table A.id=Table B.id;

 

select * from Table A full join Table B

on Table A.id=Table B.id;

 

 

 

五中的子查詢還可如下處理:

SELECT cust_name FROM Customers, Orders, OrderItems

WHERE Customers.cust_id = Orders.cust_id

 AND OrderItems.order_num = Orders.order_num

 AND prod_id = 'A';

 

 

 

 

UNION  UNION ALLunion類似合併,join類似拼接)

 

SELECT name FROM TableA 

UNION SELECT name FROM TableB;

 

SELECT name FROM TableA 

UNION ALL SELECT name FROM TableB;

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