MYsql-sql語句進階

數據類型

MySQL中定義數據字段的類型對你數據庫的優化是非常重要的。
MySQL支持多種類型,大致可以分爲三類:數值、日期/時間和字符串(字符)類型。

數值類型
在這裏插入圖片描述

日期和時間類型
在這裏插入圖片描述
字符串類型
在這裏插入圖片描述
整型

tinyint,1字節,有符號:-128~127,無符號位:0~255
smallint,2字節,有符號:-32768~32767,無符號位:0~65535
mediumint,3字節,有符號:-8388608~8388607,無符號位:0~16777215
int,4字節,有符號:-2147483648~2147483647,無符號位:0~4284967295
bigint,8字節
bool 等價於tinyint(1)  布爾型
浮點型
float([m[,d]])4字節,1.17E-38~3.4E+38 
double([m[,d]])8字節
decimal([m[,d]]) 以字符串形式表示的浮點數
字符型
char([m]):固定長度的字符,佔用m字節
varchar[(m)]:可變長度的字符,佔用m+1字節,大於255個字符:佔用m+2
tinytext,255個字符(28次方)
text,65535個字符(216次方)
mediumtext,16777215字符(224次方)
longtext,(232次方)
enum(value,value,...)1/2個字節 最多可以有65535個成員
set(value,value,...)1/2/3/4/8個字節,最多可以有64個成員

常用select命令

使用select命令查看mysql數據庫系統信息:
– 打印當前的日期和時間

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-10-21 16:22:35 |
+---------------------+
1 row in set (0.00 sec)

– 打印當前的日期

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-10-21 |
+------------+
1 row in set (0.00 sec)

– 打印當前的時間

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 16:42:00  |
+-----------+
1 row in set (0.00 sec)

– 打印當前數據庫

mysql> select database();
+------------+
| database() |
+------------+
| TEST       |
+------------+
1 row in set (0.00 sec)

– 打印MySQL版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.28    |
+-----------+
1 row in set (0.00 sec)

– 打印當前用戶

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

–查看系統信息

show variables;
show global variables;
show global variables like '%version%';
show variables like '%storage_engine%'; 默認的存儲引擎
like模糊搜索還可用戶where字句,例如
select * from students where stname like '%l%1%2%3%';

除了like 還有not like
show engines;查看支持哪些存儲引擎

–查看系統運行狀態信息

show status;
show global status like 'Thread%';

命令不知如何使用可以用help

導出,導入數據庫

方法一
導入數據庫
導入數據庫前必須創建一個空數據庫

mysql> create database app;
Query OK, 1 row affected (0.01 sec)

可以這樣建庫

[root@localhost ~]# mysql -e 'create database app' -uroot -p123456

導入數據

[root@localhost ~]# mysql -uroot -p123456 app < V6.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.  #這個警告說,在命令行寫密碼不安全

登錄查看導入的數據

mysql> use app;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> show tables;
+----------------+
| Tables_in_app  |
+----------------+
| prefix_admin   |
| prefix_app     |
| prefix_buylog  |
| prefix_key     |
| prefix_mail    |
| prefix_paylog  |
| prefix_signlog |
| prefix_user    |
+----------------+
8 rows in set (0.00 sec)

數據導入成功

方法二
建庫

mysql> create database app2;

進到新建的數據庫

mysql> use app2;
Database changed
mysql> source /root/V6.sql       #sql語句所在的位置,寫絕對路徑
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

查看數據

mysql> show tables;
+----------------+
| Tables_in_app2 |
+----------------+
| prefix_admin   |
| prefix_app     |
| prefix_buylog  |
| prefix_key     |
| prefix_mail    |
| prefix_paylog  |
| prefix_signlog |
| prefix_user    |
+----------------+
8 rows in set (0.00 sec)

導出數據庫

導出數據庫:mysqldump -u 用戶名 -p 數據庫名 > 導出的文件名

[root@localhost ~]# mysqldump -uroot -p123456 app > qq.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]#mysqldump –uroot –p123456 –B 庫名>文件.sql

-B : 導出整個庫包含建庫語句
-A:導出全部數據庫

如何把一個select的結果導出到文本
select * into outfile ‘/tmp/123.txt’ from books; 此處有個文件訪問權限問題,mysql用戶是可以訪問/tmp路徑的,所以這裏放到tmp下
select * from books into outfile ‘/tmp/456.txt’;
其實就是備份數據庫

mysql> select * from prefix_app  into outfile '/tmp/123txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

5.7版本導出報錯,可以設置my.cnf 加上secure-file-priv="/ " #這個地方可以寫路徑
配置完之後,重啓數據庫

[root@localhost ~]# systemctl restart mysqld

登錄數據庫,進到需要備份的數據庫

mysql> use app;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from prefix_app  into outfile '/tmp/123.txt';  #將查詢的結果導到txt文檔
Query OK, 1 row affected (0.00 sec)

Sql查詢語句進階

查看錶
mysql> use app;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_app  |
+----------------+
| prefix_admin   |
| prefix_app     |
| prefix_buylog  |
| prefix_key     |
| prefix_mail    |
| prefix_paylog  |
| prefix_signlog |
| prefix_user    |
+----------------+
8 rows in set (0.00 sec)

查看錶的內容

mysql> select * from prefix_app;

查看字段類型

desc 表名
mysql> desc prefix_app;

邏輯運算符:
and or not
and 且
or 或
not 非
選擇出 in_id=6的記錄

mysql> select in_id,in_uid,in_form from prefix_app where in_id=6;
+-------+--------+---------+
| in_id | in_uid | in_form |
+-------+--------+---------+
|     6 |      2 | Android |
+-------+--------+---------+
1 row in set (0.00 sec)

算術運算符

=	等於
<>	不等於  !=
>	大於
<	小於
>=	大於等於
<=	小於等於

in 運算符

IN 運算符用於 WHERE 表達式中,以列表項的形式支持多個選擇,語法如下:
WHERE column IN (value1,value2,...)
WHERE column NOT IN (value1,value2,...)

Not in 與in相反

當 IN 前面加上 NOT 運算符時,表示與 IN 相反的意思,即不在這些列表項內選擇。

找出價格大於60的記錄

mysql> select bName,price from books where price>60;

找出價格爲60的

mysql> select bName,price from books where price=60;

找出價格不等於60的

mysql> select bName,price from books where price<>60;

找出價格是60,50,70的記錄

mysql> select bName,price from books where price in (50,60,70);

找出價格不是60,50,70的記錄

mysql> select bName,price from books where price not in (50,60,70);

排序

升序:order by “排序的字段” asc 默認
降序:oredr by “排序的字段” desc

mysql> select bName,price from books where price  in (50,60,70) order by price asc;

多個字段排序

mysql> select bName,price from books where price  in (50,60,70) order by price desc,bName desc;

範圍運算

[not]between ....and....
Between and 可以使用大於小於的方式來代替,並且使用大於小於意義表述更明確
查找價格不在3060之間的書名和價格
mysql> select bName,price from books where price not between 30 and 60 order by price desc;
注:
這裏的查詢條件有三種:between。。。and,or 和 in
(30,60) >30 and <60
[30,60] >=30 and <=60

模糊匹配查詢

字段名 [not]like '通配符'  ----% 任意多個字符

查找書名中包括"程序"字樣記錄
mysql> select bName from books where bName like '%程序%';
不含有
mysql> select bName from books where bName not like '%程序%';

MySQL子查詢
概念:在select 的where條件中又出現了select
查詢中嵌套着查詢

選擇 類型名爲“網絡技術”的圖書:

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='網絡技術');
選擇類型名稱爲“黑客”的圖書;

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='黑客');

Limit限定顯示的條目

SELECT * FROM table LIMIT [offset,] rows 
                         偏移量  行數

LIMIT 子句可以被用於強制 SELECT 語句返回指定的記錄數。LIMIT 接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,第一個參數指定第一個返回記錄行的偏移量,第二個參數指定返回記錄行的最大數目。初始記錄行的偏移量是 0(而不是 1):

比如select * from table limit m,n語句

表示其中m是指記錄開始的index,從0開始,表示第一條記錄
n是指從第m+1條開始,取n條。

查出category表中第2條到第6行的記錄。
首先2到6行有2,3,4,5,6總共有5個數字,從2開始,偏移量爲1

mysql> select * from category limit 1,5;

查看所有書籍中價格中最低的三條記錄
我們對所有記錄排序以升序排列,取出前面3個來

mysql> select bName,price from books order by price asc limit 0,3;

將子查詢和限制條目,算術運算結合起來查詢
顯示字段bName ,price ;條件:找出價格比電子工業出版社出版的書中最便宜還要便宜的書。
針對這種查詢,我們一步步的來,先找出電子工業出版社出版中最便宜的書

mysql> select bName,price from books where publishing="電子工業出版社" order by price asc limit 0,1;
mysql> select bName,price from books where price<(select price from books where publishing="電子工業出版社" order by price asc limit 0,1);

或者
多行子查詢: all表示小於子查詢中返回全部值中的最小值

mysql> select bName,price from books where price<all(select price from books where publishing="電子工業出版社");

連接查詢:
以一個共同的字段,求兩張表當中符合條件的並集。 通過共同字段把這兩張表連接起來。
常用的連接:
內連接:根據表中的共同字段進行匹配
外連接分兩種:左外連接、右外鏈接。

內連接
語法:
select 字段 from 表1 inner join 表2 on 表1.字段=表2.字段
內連接:根據表中的共同字段進行匹配
測試

mysql>Select a.bname,a.price,b.btypename from books a inner join category b on a.btypeid=b.btypeid;

實際使用中inner可省略掉
跟WHERE 子句結果一樣

mysql>select a.bname,a.price,b.btypename from books a, category b where a.btypeid=b.btypeid;

外連接 (分爲左外連接;右外連接)
1.左連接: select 字段 from a表 left join b表 on 連接條件
a表是主表,都顯示。
b表從表
主表內容全都有,從表內沒有的顯示null。

mysql>Select a.bname,a.price,b.btypename from books a left join category b on a.btypeid=b.btypeid;

2.右連接:select 字段 from a表 right join b表 on 條件
a表是從表,
b表主表,都顯示。

mysql> select a.bname,b.* from books a right join category b on a.btypeid=b.btypeid;

右連接,可以多表連接

聚合函數
函數:執行特定功能的代碼塊。
算數運算函數:
Sum()求和
顯示所有圖書單價的總合

mysql> select sum(price) from books; 或select sum(price) as 圖書總價 from books;

avg()平均值:
求書籍Id小於3的所有書籍的平均價格

mysql> select avg(price) from books where bId<=3;

max() 最大值:
求所有圖書中價格最貴的書籍

mysql> select bName,max(price) from books; 這種方法是錯誤的

查一下最貴的圖書是哪本?

mysql>select bname,price from books order by desc price limit 0,3;

可見最貴書是Javascript與Jscript從入門到精通,而不是網站製作直通車

mysql>select bName,price from books where price=(select max(price) from books);

min()最小值
求所有圖書中價格便宜的書籍

mysql> select bName,price from books where price=(select min(price) from books);

count()統計記錄數
統計價格大於40的書籍數量

mysql> select count(*) from books where price>40;

Count()中還可以增加你需要的內容,比如增加distinct來配合使用

select count(distinct price) from books where price>40;

算數運算:
+ - * /
給所有價格小於40元的書籍,漲價5元

mysql> update books set price=price+5 where price<40;
給所有價格高於70元的書籍打8折
mysql> update books set price=price*0.8 where price>70;

字符串函數:
substr(string ,start,len) 截取:從start開始,截取len長.start 從1開始算起。

mysql> select substr(bTypeName,1,7) from category where bTypeId=10;
mysql>select substr(bTypeName,8,2)from category where bTypeId=10;

concat(str1,str2,str3…) 拼接。 把多個字段拼成一個字段輸出

mysql> select concat(bName,publishing) from books;
mysql> select concat(bName,"-----",publishing) from books;

大小寫轉換
upper()大寫 : 轉爲大寫輸出

mysql> select upper(bname) from books where bId=9;

這樣轉換中文會出現亂碼
lower()小寫 :轉爲小寫輸出

mysql> select lower(bName) from books where bId=10;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章