標籤:總結 book 通過 清單項目 大於 oca foreign val variable
mysql查詢語句
常用SELECT命令
列印當前的日期和時間
select now();
列印當前的日期
select curdate();
列印目前時間
select curtime();
查看目前的版本
select version();
列印目前使用者
select user();
查看當前資料庫執行個體
select database();
查看系統中可用的變數
show variables;
查看系統中全域變數
show global variables;
一般查詢系統可用變數或是全域變數都是通過like的方式來進行查詢的,因為普通查詢的內容查詢的特別多,通過like模糊查詢的方式來進行查詢,查詢的結果進行展示
show global variables like ‘%version%‘; 把全域變數中含有version的字樣全部進行展示
查看預設儲存引擎
show variables like ‘%storage_engine%‘;
mysql> show variables like ‘%storage_engine%‘;
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
| storage_engine | InnoDB |
+------------------------+--------+
如果需要將儲存引擎進行修改則使用下面的命令對儲存引擎進行修改就行
set storage_engine=MyISAM 這個就是表示將儲存引擎進行修改的方法
修改後的儲存引擎展示
mysql> show variables like ‘%storage_engine%‘;
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | MyISAM |
| storage_engine | MyISAM |
+------------------------+--------+
總結
mysql中的變數的值都是可以通過set這個來進行設定的,也就是說可以通過set來進行修改
查看支援的儲存引擎
show engines;
mysql> show engines;
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
6 rows in set (0.00 sec)
查看系統運行狀態
show status;
查看系統線程數
show status like ‘%Thread%‘;
yum安裝的mysql預設的字元集是latain1
編譯安裝的mysql是utf-8的字元集
匯出,匯入資料庫
匯出表示做了一個Database Backup
匯入表示做了一個資料庫還原
匯入資料庫
匯入資料庫前必須建立一個空資料庫
create database book; 建立資料庫
mysql -usystem -p123456 book<book.sql 把book.sql內容匯入到剛才建立的book這個庫裡面
反過來
mysql -usystem -p123456 book<book.sql 把book這個庫裡面的內容匯出到book.sql這個檔案裡面
進入資料庫刪除表使用drop
drop table books;
方法2 :類似於將sql指令碼跑了一遍
create database book;
use book;
source /root/book.sql #sql指令碼路徑
show tables;
匯出資料庫
使用mysqldump 預設存在
[[email protected] ~]# ll /usr/local/mysql/bin/mysqldump
-rwxr-xr-x 1 mysql mysql 4702840 Sep 30 22:13 /usr/local/mysql/bin/mysqldump
匯出資料庫:mysql -u使用者名稱 -p密碼 資料庫名 > 匯出的檔案名稱
mysqldump -usystem -p123456 book>/home/zyg.sql 將book這個database匯出到/home/zyg.sql這個檔案裡面,這個表示的是將整個執行個體進行匯出
這個是在linux系統裡面進行執行的
方法2:在資料庫裡面進行執行
首先進入資料庫
mysql -usystem -p123456
然後在進入database裡面
use books 進入books這個database
select * into outfile ‘/tmp/123.txt‘ from books; 將books這張表裡面的內容全部進行匯出到/tmp/123.txt這個檔案裡面。這個可以理解為select * from books 將books這張表裡面的內容都查詢出來,然後into outfile到/tmp/123.txt這個檔案裡面去,這個是將單張表裡面的內容匯出來,匯出來的路徑必須是mysql能夠進行訪問的路徑才行,所以放在tmp這個路徑下面
sql語句進階
查看錶的內容
select * from books;
select * from category;
邏輯運算子
and or not
and 且
or 或
not 非
選擇出書籍價格為(30,40,50,60)的記錄,只顯示書籍名稱,出版社,價格
mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
多條件用and串連
不等於符號:!= 或是 <> 使用這兩個符號來進行表示不等於的意思
not配合使用,比如說配合not like來進行使用等等
算術運算子:
=等於
<>不等於 !=
>大於
<小於
>=大於等於
<=小於等於
in運算子
IN 運算子用於 WHERE 運算式中,以清單項目的形式支援多個選擇,文法如下:
WHERE column IN (value1,value2,...)
WHERE column NOT IN (value1,value2,...)
mysql> select bName,publishing,price from books where price in (30,40,50,60); 條件查詢語句可以使用in,只要where條件陳述式在in的範圍之內中的任意一個就能進行執行
Not in 與in相反
當 IN 前面加上 NOT 運算子時,表示與 IN 相反的意思,即不在這些清單項目內選擇
mysql> select bName,publishing,price from books where price not in (30,40,50,60);
查詢價格大於60的記錄
select bName,price from books where price>60;
select bName,price from books where price=60;
select bName,price from books where price<>60;
or
select bName,price from books where price!=60;
找出價格為50,60,70的書
select bName,price from books where price in (50,60,70);
找出價格不為50,60,70的書
select bName,price from books where price not in (50,60,70);
排序
升序:order by “排序的欄位” asc 預設
降序:oredr by “排序的欄位” desc
升序
select bName,price from books where price in (50,60,70) order by price asc;
降序
select bName,price from books where price in (50,60,70) order by price desc;
多組排序是先按照前面的那個排序條件進行排序
select bName,price from books where price in (50,60,70) order by price desc,bNa
me asc; 那麼這個排序的方式是先按照price的順序進行排序,先進行最前面的進行排序,可以滿足多個欄位排序
範圍運算:
[not]between......and.....
Between and 可以使用大於小於的方式來代替,並且使用大於小於意義表述更明確,這個有時候會表述不明確,平時都是使用下面的表示方法
一般使用下面的符號進行表示
(30,60)表示30和60之間的數字但是不包括30和60 >30 and <60
[30,60] 表示30和60之間的數字並且包括30和60 >=30 and <=60
select bName,price from books where price >=30 and price <= 60 order by price d
esc;
select bName,price from books where price between 30 and 60 order by price des
c;
上面這兩條語句的意思是一樣的,但是一般都是使用上面的那條語句
模糊比對查詢:
欄位名 [not]like ‘萬用字元‘ ----》% 任意多個字元
mysql子查詢
概念:在select 的where條件中又出現了select
查詢中嵌套著查詢
select bName,bTypeId from books where bTypeId in (select bTypeId from category where bTypeName in (‘駭客‘,‘網路技術‘)); 多表之間的子查詢語句
LIMIT 限定顯示的條目:通過limit來進行條件查詢,limit進行取值查詢
select * from table limit [offset,]rows
位移量 行數
LIMIT 子句可以被用於強制 SELECT 語句返回指定的記錄數。LIMIT 接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,第一個參數指定第一個返回記錄行的位移量,第二個參數指定返回記錄行的最大數目。初始記錄行的位移量是 0(而不是 1):
offset表示的是位移量,預設是0,表示不位移
比如select * from table limit m,n語句
表示其中m是指記錄開始的index,從0開始,表示第一條記錄
n是指從第m+1條開始,取n條。
mysql> select * from category limit 1,5; 這個表示除去第一個,從第二個開始找5行
+---------+--------------+
| bTypeId | bTypeName |
+---------+--------------+
| 2 | 網站 |
| 3 | 3D動畫 |
| 4 | linux學習 |
| 5 | Delphi學習 |
| 6 | 駭客 |
+---------+--------------+
查看所有書籍中價格中最低的三條記錄
我們對所有記錄排序以升序排列,取出前面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="電子工業出版社");
串連查詢
以一個共同的欄位,求兩張表當中合格並集。 通過共同欄位把這兩張表串連起來。
常用的串連:
右邊的表關聯到左邊的表,以左邊的表為基準這個叫做左串連,反過來叫右串連
內串連:根據表中的共同欄位進行匹配
外串連分兩種:左外串連、右外連結。
內串連 inner
文法:
select 欄位 from 表1 inner join 表2 on 表1.欄位=表2.欄位
select 欄位 from [表1 inner join 表2] on 條件
內串連:根據表中的共同欄位進行匹配
mysql查詢語句