MySQL基礎部分總結

MySQL

1、選擇數據庫

  1. use dbname
  2. show databases;

2、數據表

  1. show tables
  2. mysql> show columns from customers;
  3. mysql> desc customers;

3、show 語句

  1. show status
  2. show create databases
  3. show create table
  4. show grants

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,4

4-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 條件

相關操作符:

  1. = 等于
  2. <> 不等于
  3. != 不等于
  4. < 小于
  5. > 大于
  6. >= 大于或者等于
  7. <= 小于或等于
  8. between 兩者之間 and
and 的優先級大于or,需要處理or,則需要括號
mysql> select prod_price,prod_name from  products where prod_price = 2.50;
mysql> select prod_price,prod_name from  products where prod_price  between 5 and 10;
//  IS NULL
mysql> select cust_id from customers where cust_email is null;
重點:空值檢查
空值既是: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,如果要按理想執行,加括號!

mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002 and prod_price >= 10;
mysql> select prod_id,prod_price,prod_name from products where (vend_id = 1003 or vend_id = 1002 )and prod_price >= 10;

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 作為產品名的行。**

mysql> select prod_id,prod_price,prod_name from products where prod_name  LIKE 'jet%';mysql> select prod_id,prod_price,prod_name from products where prod_name  LIKE '%anv%';

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、計算字段

  1. concat 合并 講兩個字段合并成一個新的字段
mysql> select concat (vend_name , 'C',vend_country,')') from vendors order by vend_name;
+-------------------------------------------+
| concat (vend_name , 'C',vend_country,')') |
+-------------------------------------------+
| ACMECUSA)                                 |
| Anvils R UsCUSA)                          |
| Furball Inc.CUSA)                         |
| Jet SetCEngland)                          |
| Jouets Et OursCFrance)                    |
| LT SuppliesCUSA)                          |
+-------------------------------------------+
6 rows in set (0.00 sec)
  1. rtrim (ltrim ,trim) 去掉空格
mysql> select concat (rtrim(vend_name) , 'C',vend_country,')') from vendors order by vend_name;
  1. as 別名
mysql> select concat (rtrim(vend_name) , '(',rtrim(vend_country),')') as vend_title   from vendors order by vend__name;
  1. 計算

+、-、* 、\

mysql> select quantity*item_price as expand_price from orderitems where order_num =20005;

11、函數

  1. trim、ltrim、rtrim 去掉空值
  2. Upper 轉為大寫

mysql> select vend_name,upper(vend_name) as ven_name_upcase from vendors order by vend_name;

11-2 時間函數

  1. AddDate() 增加一個日期(天、周等)
  2. AddTime() 增加一個時間(時、分等)
  3. CurDate() 返回當前日期
  4. CurTime() 返回當前時間
  5. ==Date() 返回日期時間的日期部分==
  6. DateDiff() 計算兩個日期之差
  7. Date_Add() 高度靈活的日期運算函數
  8. Date_Format() 返回一個格式化的日期或時間串
  9. Day() 返回一個日期的天數部分
  10. DayOfWeek() 對于一個日期,返回對應的星期幾
  11. Hour() 返回一個時間的小時部分
  12. Minute() 返回一個時間的分鐘部分
  13. Month() 返回一個日期的月份部分
  14. Now() 返回當前日期和時間
  15. Second() 返回一個時間的秒部分
  16. Time() 返回一個日期時間的時間部分
  17. Year() 返回一個日期的年份部分

取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 數值處理函數

  1. Abs() 返回一個數的絕對值
  2. Cos() 返回一個角度的余弦
  3. Exp() 返回一個數的指數值
  4. Mod() 返回除操作的余數
  5. Pi() 返回圓周率
  6. Rand() 返回一個隨機數
  7. Sin() 返回一個角度的正弦
  8. Sqrt() 返回一個數的平方根
  9. Tan() 返回一個角度的正切

11-5 聚集函數

  1. AVG() 返回某列的平均值
  2. COUNT() 返回某列的行數
  3. MAX() 返回某列的最大值
  4. MIN() 返回某列的最小值
  5. SUM() 返回某列值之和
  6. DISTINCT
mysql> select avg(prod_price) as avg_price from products;

分組數據

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必知必會》

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388152.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388152.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388152.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

BZOJ2503: 相框

Description P大的基礎電路實驗課是一個無聊至極的課。每次實驗&#xff0c;T君總是提前完成&#xff0c;管理員卻不讓T君離開&#xff0c;T君只能干坐在那兒無所事事。先說說這個實驗課&#xff0c;無非就是把幾根導線和某些元器件&#xff08;電阻、電容、電感等&#xff09;…

泰坦尼克號 數據分析_第1部分:泰坦尼克號-數據分析基礎

泰坦尼克號 數據分析My goal was to get a better understanding of how to work with tabular data so I challenged myself and started with the Titanic -project. I think this was an excellent way to learn the basics of data analysis with python.我的目標是更好地了…

Imperva開源域目錄控制器,簡化活動目錄集成

Imperva已公開發布域目錄控制器&#xff08;Domain Directory Controller&#xff0c;DDC&#xff09;的源代碼&#xff0c;這是一個Java庫&#xff0c;用于簡化常見的Active Directory集成。 與Java的LdapContext不同&#xff0c;這個庫構建在Apache Directory LDAP之上&#…

2018.10.24 NOIP模擬 小 C 的序列(鏈表+數論)

傳送門 考慮到a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])是可以分成最多logloglog段且段內的數都是相同的。 那么我們用…

vba數組dim_NDArray — —一個基于Java的N-Dim數組工具包

vba數組dim介紹 (Introduction) Within many development languages, there is a popular paradigm of using N-Dimensional arrays. They allow you to write numerical code that would otherwise require many levels of nested loops in only a few simple operations. Bec…

Nodejs教程08:同時處理GET/POST請求

示例代碼請訪問我的GitHub&#xff1a; github.com/chencl1986/… 同時處理GET/POST請求 通常在開發過程中&#xff0c;同一臺服務器需要接收多種類型的請求&#xff0c;并區分不同接口&#xff0c;向客戶端返回數據。 最常用的方式&#xff0c;就是對請求的方法、url進行區分判…

關于position的四個標簽

四個標簽是static&#xff0c;relative&#xff0c;absolute&#xff0c;fixed。 static 該值是正常流&#xff0c;并且是默認值&#xff0c;因此你很少看到&#xff08;如果存在的話&#xff09;指定該值。 relative&#xff1a;框的位置能夠相對于它在正常流中的位置有所偏移…

python算法和數據結構_Python中的數據結構和算法

python算法和數據結構To至 Leonardo da Vinci達芬奇(Leonardo da Vinci) 介紹 (Introduction) The purpose of this article is to give you a panorama of data structures and algorithms in Python. This topic is very important for a Data Scientist in order to help …

CSS:元素塌陷問題

2019獨角獸企業重金招聘Python工程師標準>>> 描述&#xff1a; 在文檔流中&#xff0c;父元素的高度默認是被子元素撐開的&#xff0c;也就是子元素多高&#xff0c;父元素就多高。但是當子元素設置浮動之后&#xff0c;子元素會完全脫離文檔流&#xff0c;此時將會…

Celery介紹及常見錯誤

celery 情景&#xff1a;用戶發起request&#xff0c;并等待response返回。在本些views中&#xff0c;可能需要執行一段耗時的程序&#xff0c;那么用戶就會等待很長時間&#xff0c;造成不好的用戶體驗&#xff0c;比如發送郵件、手機驗證碼等。 使用celery后&#xff0c;情況…

python dash_Dash是Databricks Spark后端的理想基于Python的前端

python dash&#x1f4cc; Learn how to deliver AI for Big Data using Dash & Databricks this recorded webinar with Peter Kim of Plotly and Prasad Kona of Databricks.this通過Plotly的Peter Kim和Databricks的Prasad Kona的網絡研討會了解如何使用Dash&#xff06…

js里的數據類型轉換

1、類型轉換 轉換為字符串 - String(x)- x.toString(x, 10)- x 轉換為數字 - Number(x)- parseInt(x, 10) - parseFloat(x) - x - 0- x 轉換為boolean - Boolean(x)- !!x 2、falsy值&#xff08;false&#xff09; - 0- NaN- - null- undefined 3、內存圖 - object存儲的是地址…

Eclipse 插件開發遇到問題心得總結

Eclipse 插件開發遇到問題心得總結 Posted on 2011-07-17 00:51 季楓 閱讀(3997) 評論(0) 編輯 收藏1、Eclipse 中插件開發多語言的實現 為了使用 .properties 文件&#xff0c;需要在 META-INF/MANIFEST.MF 文件中定義&#xff1a; Bundle-Localization: plugin 這樣就會…

/src/applicationContext.xml

<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:context"http://www.springframework.org/schema…

在Python中查找子字符串索引的5種方法

在Python中查找字符串中子字符串索引的5種方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() str.find() str.rfind() str.rfind() str.index() str.index() str.rindex() str.rindex() re.search() re.search() str.find() (str.find()) …

[LeetCode] 3. Longest Substring Without Repeating Characters 題解

問題描述 輸入一個字符串&#xff0c;找到其中最長的不重復子串 例1&#xff1a; 輸入&#xff1a;"abcabcbb" 輸出&#xff1a;3 解釋&#xff1a;最長非重復子串為"abc" 復制代碼例2&#xff1a; 輸入&#xff1a;"bbbbb" 輸出&#xff1a;1 解…

WPF中MVVM模式的 Event 處理

WPF的有些UI元素有Command屬性可以直接實現綁定&#xff0c;如Button 但是很多Event的觸發如何綁定到ViewModel中的Command呢&#xff1f; 答案就是使用EventTrigger可以實現。 繼續上一篇對Slider的研究&#xff0c;在View中修改Interaction. <i:Interaction.Triggers>&…

Eclipse 插件開發 向導

閱讀目錄 最近由于特殊需要&#xff0c;開始學習插件開發。   下面就直接弄一個簡單的插件吧!   1 新建一個插件工程   2 創建自己的插件名字&#xff0c;這個名字最好特殊一點&#xff0c;一遍融合到eclipse的時候&#xff0c;不會發生沖突。   3 下一步&#xff0c;進…

線性回歸 假設_線性回歸的假設

線性回歸 假設Linear Regression is the bicycle of regression models. It’s simple yet incredibly useful. It can be used in a variety of domains. It has a nice closed formed solution, which makes model training a super-fast non-iterative process.線性回歸是回…

ES6模塊與commonJS模塊的差異

參考&#xff1a; 前端模塊化 ES6 在語言標準的層面上&#xff0c;實現了模塊功能&#xff0c;而且實現得相當簡單&#xff0c;旨在成為瀏覽器和服務器通用的模塊解決方案。 其模塊功能主要由兩個命令構成&#xff1a;export和import。export命令用于規定模塊的對外接口&#x…