MySQL基础部分总结
MySQL
1、選擇數據庫
2、數據表
3、show 語句
4、select 檢索
4.1.1版本后不再區分大小寫,但是為了容易閱讀和調試,建議還是使用。
mysql> select cust_name from customers; mysql> select cust_name cust_status from customers; mysql> select distinct vend_id from products; mysql> select prod_name from products limit 5; mysql> select prod_name from products limit 5,5; //分頁問題 從行0開始計數,limit5,5是從第5行開始(不包括第五行),取5行,結果是:6:10行 因為這個很容易引起誤解,所以MySQL5開始支持另一個語法:limit 4 offset 3,意思是從行3開始取4行,等同于limit 3,44-1、排序數據
//單個字段排序 mysql> select prod_name from products order by prod_name; //多個字段排序,如果第一個字段全部唯一則第二個字段就不會生效 mysql> select prod_id,prod_price,prod_name from products order by prod_price ,prod_name;4-2、指定排序方向
- desc 降序
- asc 升序-默認
注意順序,from>ordrr by >limit
mysql> select prod_id,prod_price,prod_name from products order by prod_price desc; mysql> select prod_id,prod_price,prod_name from products order by prod_price asc; mysql> select prod_price from products order by prod_price desc limit 1;5、where 條件
相關操作符:
空值既是:NULL
MySQL中判斷是否是空值的子句是: IS NULL
example:
mysql> select cust_id FROM customers where cust_email IS NULL; +---------+ | cust_id | +---------+ | 10002 | | 10005 | +---------+6、where 數據過濾
(logical operator)邏輯操作符:and - or
mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 and prod_price<= 10; mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002;運算符優先級問題:
下列SQL中實際先運行 vend_id = 1002 and prod_price >= 10;,再運行vend_id = 1003.因為and的優先級大于or,如果要按理想執行,加括號!
6-1、 in操作符 (not in)
mysql> select prod_id,prod_price,prod_name from products where vend_id in (1002,1003) order by prod_name;6-2、 or操作符
mysql> select prod_id,prod_price,prod_name from products where vend_id not in (1002,1003) order by prod_name;7、用通配符過濾
like 和 _ 的區別是后者只能匹配一個字符
7-1、like
**注意NULL 雖然似乎 % 通配符可以匹配任何東西,但有一個例
外,即 NULL 。即使是 WHERE prod_name LIKE '%' 也不能匹配
用值 NULL 作為產品名的行。**
7-2、_
mysql> select prod_id,prod_price,prod_name from products where prod_name LIKE '_ ton anvil';8、正則表達式
like是匹配全部,REGEXP可以匹配全部和部分
mysql> select prod_name from products where prod_name ='JetPack 1000'; +--------------+ | prod_name | +--------------+ | JetPack 1000 | +--------------+ 1 row in set (0.00 sec)mysql> select prod_name from products where prod_name REGEXP '1000'; +--------------+ | prod_name | +--------------+ | JetPack 1000 | +--------------+ 1 row in set (0.00 sec)默認不區分大小寫,需要區分大小寫binary
mysql> select prod_name from products where prod_name REGEXP 'jetpack .000'; mysql> select prod_name from products where prod_name REGEXP binary 'JetPack .000';10、計算字段
+、-、* 、\
mysql> select quantity*item_price as expand_price from orderitems where order_num =20005;11、函數
11-2 時間函數
取9月某一天的數據
mysql> select cust_id,order_num from orders where Date(order_date) = '2005-09-01'; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | +---------+-----------+ 1 row in set (0.00 sec)取9月整個月的訂單
mysql> select cust_id,order_num from orders where Date(order_date) between '2005-09-01' and '2005-09-30'; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10003 | 20006 | | 10004 | 20007 | +---------+-----------+ 3 rows in set (0.00 sec)mysql> select cust_id,order_num from orders where Year(order_date) and month(order_date) = 9; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10003 | 20006 | | 10004 | 20007 | +---------+-----------+ 3 rows in set (0.00 sec)11-4 數值處理函數
11-5 聚集函數
分組數據
GROUP BY子句和HAVING子句
mysql> select vend_id,count(*) as num_prods from products group by vend_id; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1002 | 2 | | 1003 | 7 | | 1005 | 2 | +---------+-----------+ 4 rows in set (0.00 sec)mysql> select vend_id,count(*) as num_prods from products group by vend_id with rollup; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1002 | 2 | | 1003 | 7 | | 1005 | 2 | | NULL | 14 | +---------+-----------+ 5 rows in set (0.00 sec) having唯一的差別是 WHERE過濾行,而HAVING過濾分組。WHERE在數據 分組前進行過濾,HAVING在數據分組后進行過濾 mysql> select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=2; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1002 | 2 | | 1003 | 7 | | 1005 | 2 | +---------+-----------+ 4 rows in set (0.00 sec)mysql> select vend_id,count(*) as num_prods from products where prod_price>=10 group by vend_id having count(*)>=2; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1003 | 4 | | 1005 | 2 | +---------+-----------+ 2 rows in set (0.00 sec)mysql> select order_num ,sum(quantity*item_price) as ordertotal from orderitems -> group by order_num -> having sum(quantity*item_price) >= 50 -> order by ordertotal; +-----------+------------+ | order_num | ordertotal | +-----------+------------+ | 20006 | 55.00 | | 20008 | 125.00 | | 20005 | 149.87 | | 20007 | 1000.00 | +-----------+------------+ 4 rows in set (0.00 sec) 順序
- select
- from
- where
- group by
- having
- order by
- limit
12 子查詢
mysql> select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='TNT2'); +---------+ | cust_id | +---------+ | 10001 | | 10004 | +---------+15 連接表
笛卡兒積(cartesian product)
如果將兩個表同時作為數據源(from后的表名),不加任何的匹配條件,那么產生的結果集就是一個迪卡爾積。
迪卡爾積的結果沒有意義,但是迪卡爾積是聯合查詢、連接查詢的基礎。
1. 交叉連接 cross join
使用表A中的1條記錄去表B中連接所有的記錄,就是笛卡爾積
2. 內連接
select 字段列表 from 表A 【inner】 join 表B ,匹配到的成功的記錄
3. 外連接 分為左連接和右連接,
左連接保留左邊的所有,右邊匹配到的部分
4. using關鍵字
在進行連接時,如果進行連接的兩個字段的名子相同,則可以使用using using('cid') 當前筆記出自 《MySQL必知必會》總結
以上是生活随笔為你收集整理的MySQL基础部分总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到大鱼吃小鱼是什么征兆
- 下一篇: 梦到洗好多衣服是什么意思