您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mariadb的單表查詢方法的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Mariadb的單表查詢方法文章都會有所收獲,下面我們一起來看看吧。
MariaDB Server 是最流行的開源關系型數據庫之一。它由 MySQL 的原始開發者制作,并保證保持開源。它是大多數云產品的一部分,也是大多數Linux發行版的默認配置。MariaDB 被設計為 MySQL 的直接替代產品,具有更多功能,新的存儲引擎,更少的錯誤和更好的性能。
前幾天記了下創建、刪除、修改數據庫,表啊之類的學習筆記,今天終于要開始查詢了,查詢數據嘛~在我心里反正挺難的,畢竟SQL不好寫,腦袋笨啊。
首先呢,Mysql官方提供了一個數據庫實例給我們用,那~就是大名鼎鼎的world.sql.
進入數據庫后,輸入SOURCE /PATH/world.sql
例如:
MariaDB [world]> SOURCE /root/world.sql MariaDB [world]> SHOW DATABASES; //可以看到我們的world庫了 +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | world | +--------------------+ MariaDB [world]> USE world; //切換數據庫 Database changed MariaDB [world]> SHOW TABLES; //有三張表供我們使用 +-----------------+ | Tables_in_world | +-----------------+ | city | | country | | countrylanguage | +-----------------+ 3 rows in set (0.00 sec) MariaDB [world]> DESC city; //city表的結構 +-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | Name | char(35) | NO | | | | | CountryCode | char(3) | NO | MUL | | | | District | char(20) | NO | | | | | Population | int(11) | NO | | 0 | | +-------------+----------+------+-----+---------+----------------+
非常簡單的單表查詢
先看下SELECT語句的語法吧:
SELECT [ALL | DISTINCT | DISTINCTROW] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [ FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] [export_options]
其中select_expr可以為其下值:
這個就沒什么說的了,非常非常簡單~
MariaDB [world]> SELECT * FROM city;
別名是個很有用的功能呢~特別是多表查詢的時候
MariaDB [world]> SELECT ID as 'iid' FROM city LIMIT 2; +-----+ | iid | +-----+ | 129 | | 1 | +-----+
之前也有用到LIMIT,LIMIT 關鍵字接收兩個參數,第一個參數是偏移位置,第二個參數是顯示行數
MariaDB [world]> SELECT * FROM city LIMIT 2,2; //這里默認是按主鍵排序的,所以2表示顯示第3行數據,一共顯示兩行 +----+----------------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +----+----------------+-------------+----------+------------+ | 3 | Herat | AFG | Herat | 186800 | | 4 | Mazar-e-Sharif | AFG | Balkh | 127800 | +----+----------------+-------------+----------+------------+ 2 rows in set (0.01 sec)
ODER BY之前也有用到,根據指定列排序嘛~ ASC是順序顯示(默認,從小到大),DESC是倒序顯示,當指定了多個列時,先按前面的列排序(分了一組),然后再在組內按后面的列排序,依次類推。以下就顯示了人口最多的兩個國家:
MariaDB [world]> SELECT * FROM city ORDER BY Population DESC LIMIT 2; +------+-----------------+-------------+-------------+------------+ | ID | Name | CountryCode | District | Population | +------+-----------------+-------------+-------------+------------+ | 1024 | Mumbai (Bombay) | IND | Maharashtra | 10500000 | | 2331 | Seoul | KOR | Seoul | 9981619 | +------+-----------------+-------------+-------------+------------+ 2 rows in set (0.01 sec)
SELECT DISTINCT 列名 FROM 表名; SELECT DISTINCT * FROM city; //當然,這個city表中是沒有重復數據的
那我們創建一個表測試一下吧:
MariaDB [world]> CREATE TABLE test(name VARCHAR(50),pass VARCHAR(50)); MariaDB [world]> INSERT INTO test VALUES //插入了4條測試數據 -> ('test','123'), -> ('test','321'), -> ('test','123'), -> ('test1','123'); MariaDB [world]> SELECT DISTINCT * FROM test; +-------+------+ | name | pass | +-------+------+ | test | 123 | | test | 321 | | test1 | 123 | +-------+------+ MariaDB [world]> SELECT DISTINCT name,pass FROM test; +-------+------+ | name | pass | +-------+------+ | test | 123 | | test | 321 | | test1 | 123 | MariaDB [world]> SELECT DISTINCT pass,name FROM test; +------+-------+ | pass | name | +------+-------+ | 123 | test | | 321 | test | | 123 | test1 | +------+-------+
所以,完全相同的行才會被當作重復數據排除掉。
MariaDB [world]> SELECT 'The City Info:', //顯示一列常量,當然也可是變量 Name, //普通的一列 CountryCode AS Country, //使用別名 Population*100 AS Population , //對人口放大100倍 MD5(ID) AS ID //使用MD5函數生成ID這一列的MD5值 FROM city ORDER BY Population DESC LIMIT 2; +----------------+-----------------+---------+------------+----------------------------------+ | The City Info: | Name | Country | Population | ID | +----------------+-----------------+---------+------------+----------------------------------+ | The City Info: | Mumbai (Bombay) | IND | 1050000000 | 021bbc7ee20b71134d53e20206bd6feb | | The City Info: | Seoul | KOR | 998161900 | 273448411df1962cba1db6c05b3213c9 | +----------------+-----------------+---------+------------+----------------------------------+ 2 rows in set (0.01 sec)
使用WHERE子句過濾結果集
上面的內容,我們只是控制了顯示哪些列,而并沒有控制顯示哪些行,當然,LIMIT可能算是控制了顯示哪些行;但經常我們會有更復雜的要求,比如查找所有人口大于1,000,000的城市,這時候就可以使用WHERE子句來控制顯示哪些行了。
當然,這些過濾動作可以放在業務層來做,可是,我們費了很大的力氣傳輸了數萬條數據卻只用到其中的一條,有必要嘛?而且,誰能保證業務層的過濾算法能比數據庫管理系統中的更高效呢?所以~一般很少有人在業務層進行數據過濾。
[WHERE where_condition]
使用WHERE子句僅需后面跟上where_condition即可,where_condition包括操作符、函數等,只要這行數據使這個表達式為TRUE則顯示此行:
MariaDB [world]> SELECT * FROM city WHERE Population >= 1000000; +------+--------------------------+-------------+----------------------+------------+ | ID | Name | CountryCode | District | Population | +------+--------------------------+-------------+----------------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 35 | Alger | DZA | Alger | 2168000 | | 56 | Luanda | AGO | Luanda | 2022000 | | 69 | Buenos Aires | ARG | Distrito Federal | 2982146 | ................................人工省略............................................. | 3798 | Phoenix | USA | Arizona | 1321045 | | 3799 | San Diego | USA | California | 1223400 | | 3800 | Dallas | USA | Texas | 1188580 | | 3801 | San Antonio | USA | Texas | 1144646 | | 4068 | Harare | ZWE | Harare | 1410000 | +------+--------------------------+-------------+----------------------+------------+ 238 rows in set (0.09 sec) //可以看到,有238個城市的人口大于1000000
當給定列的值為NULL時返回TRUE,否則返回FALSE
MariaDB [world]> SELECT * FROM city WHERE CountryCode IS NULL; Empty set (0.00 sec) MariaDB [world]> SELECT NULL IS NULL; //僅當給定值為NULL時,返回TRUE +--------------+ | NULL IS NULL | +--------------+ | 1 | +--------------+ 1 row in set (0.01 sec)
表達式(測試條件)1 AND 表達式(測試條件)2 : 當表達式1和表達式2都為TRUE時,整個表達式才成立
查詢中國所有人口大于100000的城市:
MariaDB [world]> SELECT * FROM city WHERE CountryCode = 'CHN' AND Population > 100000; +------+---------------------+-------------+----------------+------------+ | ID | Name | CountryCode | District | Population | +------+---------------------+-------------+----------------+------------+ | 1890 | Shanghai | CHN | Shanghai | 9696300 | | 1891 | Peking | CHN | Peking | 7472000 | .............................手工省略..................................... | 2228 | Zhucheng | CHN | Shandong | 102134 | | 2229 | Kunshan | CHN | Jiangsu | 102052 | | 2230 | Haining | CHN | Zhejiang | 100478 | +------+---------------------+-------------+----------------+------------+ 341 rows in set (0.00 sec) //當一條數據同時滿足這兩個條件才會被顯示出來
表達式1 OR 表達式2: 任一表達式為TRUE時,整個表達式都為TRUE ,當表達式1已經為TRUE時不再判斷表達式2
列出所有北京和上海的城市:
一個城市所屬的地區,不可能又屬于北京又屬于上海,所以是上海和北京城市的并集:
MariaDB [world]> SELECT * FROM city WHERE District = ‘Peking’ OR District = ‘Shanghai’;
+------+-----------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +------+-----------+-------------+----------+------------+ | 1890 | Shanghai | CHN | Shanghai | 9696300 | | 1891 | Peking | CHN | Peking | 7472000 | | 2236 | Tong Xian | CHN | Peking | 97168 | +------+-----------+-------------+----------+------------+ 3 rows in set (0.00 sec)
當條件過多時,可以結合的使用與、或操作,比如:
查詢北京或浙江人口大于1000000的城市:
MariaDB [world]> SELECT * FROM city WHERE District = 'Peking' OR District = 'Zhejiang' AND Population > 1000000; +------+-----------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +------+-----------+-------------+----------+------------+ | 1891 | Peking | CHN | Peking | 7472000 | | 1905 | Hangzhou | CHN | Zhejiang | 2190500 | | 1915 | Ningbo | CHN | Zhejiang | 1371200 | | 2236 | Tong Xian | CHN | Peking | 97168 | +------+-----------+-------------+----------+------------+ 4 rows in set (0.00 sec) //邏輯操作是從左向右的二目操作符,所以首先判斷是District = 'Peking' OR District = 'Zhejiang'這個條件,當這個條件判斷完后得到的TRUE或FALSE,再用這個布爾值與 AND Population > 1000000 進行與操作。
當聯合使用AND和OR時還是比較推薦使用()的,這樣不容易亂。
查詢浙江人口小于100000且大于10000的城市:
MariaDB [world]> SELECT * FROM city WHERE District = 'Zhejiang' AND ( Population > 10000 AND Population in set (0.00 sec) //雖然這個例子看起來沒必要使用()吧....干笑....
今天突然翻文檔發現一個東西,AND的優先級要比OR優先級高,所以,看如下例子:
查詢浙江或北京人口大于100000的城市:
MariaDB [world]> SELECT * FROM city WHERE District = 'Peking' OR District = 'Zhejiang' AND Population > 100000; +------+-----------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +------+-----------+-------------+----------+------------+ | 1891 | Peking | CHN | Peking | 7472000 | | 1905 | Hangzhou | CHN | Zhejiang | 2190500 | | 1915 | Ningbo | CHN | Zhejiang | 1371200 | ...................手工省略............................... | 2199 | Yuyao | CHN | Zhejiang | 114065 | | 2230 | Haining | CHN | Zhejiang | 100478 | | 2236 | Tong Xian | CHN | Peking | 97168 | //看這一行,為什么人口97168被篩選出來了呢? +------+-----------+-------------+----------+------------+ 16 rows in set (0.00 sec) //原因是District = 'Zhejiang' AND Population > 100000 為FALSE,然后再與其前的OR進行運算,而這條數據正好地區是北京。 //所以想要的正確篩選數據,需要加一個括號
一個值滿足一段連續的范圍時為TRUE否則為FALSE
查詢ID范圍在1-10的城市:
MariaDB [world]> SELECT * FROM city WHERE ID BETWEEN 1 AND 10; +----+----------------+-------------+---------------+------------+ | ID | Name | CountryCode | District | Population | +----+----------------+-------------+---------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 2 | Qandahar | AFG | Qandahar | 237500 | | 3 | Herat | AFG | Herat | 186800 | | 4 | Mazar-e-Sharif | AFG | Balkh | 127800 | | 5 | Amsterdam | NLD | Noord-Holland | 731200 | | 6 | Rotterdam | NLD | Zuid-Holland | 593321 | | 7 | Haag | NLD | Zuid-Holland | 440900 | | 8 | Utrecht | NLD | Utrecht | 234323 | | 9 | Eindhoven | NLD | Noord-Brabant | 201843 | | 10 | Tilburg | NLD | Noord-Brabant | 193238 | +----+----------------+-------------+---------------+------------+ 10 rows in set (0.00 sec)
剛用Name BETWEEN ‘abc’ AND ‘efg’ 作為條件篩選了一下,竟然能篩選出900多行數據,不知道什么原理。
好像只匹配了第一個字符的ASCII值,這個東西實在想不明白有什么場景會把字符用上….
BETWEEN 1 AND 10 相當于 >=1 AND
當一個值屬于一段離散數據之中時為TRUE,例如 1 IN (2,3) 明顯1不等于2不等于3,所以為FALSE
查詢ID屬于1,3,5,7,9的城市:
MariaDB [world]> SELECT * FROM city WHERE ID IN (1,3,5,7,9); +----+-----------+-------------+---------------+------------+ | ID | Name | CountryCode | District | Population | +----+-----------+-------------+---------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 3 | Herat | AFG | Herat | 186800 | | 5 | Amsterdam | NLD | Noord-Holland | 731200 | | 7 | Haag | NLD | Zuid-Holland | 440900 | | 9 | Eindhoven | NLD | Noord-Brabant | 201843 | +----+-----------+-------------+---------------+------------+ 5 rows in set (0.01 sec)
查詢北京、浙江、河南的所有城市:
MariaDB [world]> SELECT * FROM city WHERE District IN ('Peking','Zhejiang','Henan'); +------+--------------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +------+--------------+-------------+----------+------------+ | 1891 | Peking | CHN | Peking | 7472000 | | 1905 | Hangzhou | CHN | Zhejiang | 2190500 | | 1906 | Zhengzhou | CHN | Henan | 2107200 | | 1915 | Ningbo | CHN | Zhejiang | 1371200 | | 1934 | Luoyang | CHN | Henan | 760000 | | 1951 | Kaifeng | CHN | Henan | 510000 | ......................手工省略............................... | 2214 | Cixi | CHN | Zhejiang | 107329 | | 2230 | Haining | CHN | Zhejiang | 100478 | | 2236 | Tong Xian | CHN | Peking | 97168 | | 2242 | Yuzhou | CHN | Henan | 92889 | | 2246 | Linhai | CHN | Zhejiang | 90870 | | 2252 | Huangyan | CHN | Zhejiang | 89288 | +------+--------------+-------------+----------+------------+ 36 rows in set (0.01 sec)
所以,IN操作符跟OR的功能很類似,比如District IN (‘Peking’,’Zhejiang’,’Henan’)等于District = ‘Peking’ OR District = ‘Zhejiang’ OR District = ‘Henan’
將其原本的布爾值進行邏輯非操作后再判斷
比如:查詢ID
ID=10
MariaDB [world]> SELECT * FROM city WHERE NOT ID >=10; +----+----------------+-------------+---------------+------------+ | ID | Name | CountryCode | District | Population | +----+----------------+-------------+---------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 2 | Qandahar | AFG | Qandahar | 237500 | | 3 | Herat | AFG | Herat | 186800 | | 4 | Mazar-e-Sharif | AFG | Balkh | 127800 | | 5 | Amsterdam | NLD | Noord-Holland | 731200 | | 6 | Rotterdam | NLD | Zuid-Holland | 593321 | | 7 | Haag | NLD | Zuid-Holland | 440900 | | 8 | Utrecht | NLD | Utrecht | 234323 | | 9 | Eindhoven | NLD | Noord-Brabant | 201843 | +----+----------------+-------------+---------------+------------+ 9 rows in set (0.00 sec)
所以,NOT可以用作以上任何的操作,比如,ID范圍不在10-4000的城市:
MariaDB [world]> SELECT * FROM city WHERE ID NOT BETWEEN 10 AND 4000; +------+----------------------+-------------+----------------+------------+ | ID | Name | CountryCode | District | Population | +------+----------------------+-------------+----------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 2 | Qandahar | AFG | Qandahar | 237500 | | 3 | Herat | AFG | Herat | 186800 | .............................手工省略....................................... | 4077 | Jabaliya | PSE | North Gaza | 113901 | | 4078 | Nablus | PSE | Nablus | 100231 | | 4079 | Rafah | PSE | Rafah | 92020 | +------+----------------------+-------------+----------------+------------+ 88 rows in set (0.00 sec)
使用LIKE可以檢索符合通配符的字符串,有如下兩個字符串:
搜索名稱以T開頭的城市:
MariaDB [world]> SELECT * FROM city WHERE Name LIKE 'Y%'; +------+--------------------+-------------+----------------+------------+ | ID | Name | CountryCode | District | Population | +------+--------------------+-------------+----------------+------------+ | 126 | Yerevan | ARM | Yerevan | 1248700 | | 516 | York | GBR | England | 104425 | | 955 | Yogyakarta | IDN | Yogyakarta | 418944 | | 1220 | Yamuna Nagar | IND | Haryana | 144346 | | 1300 | Yeotmal (Yavatmal) | IND | Maharashtra | 108578 | | 1396 | Yazd | IRN | Yazd | 326776 | ...........................手工省略...................................... | 3888 | Yonkers | USA | New York | 196086 | +------+--------------------+-------------+----------------+------------+ 63 rows in set (0.00 sec)
搜索名稱為三個字母的城市:
MariaDB [world]> SELECT * FROM city WHERE Name LIKE '___'; +------+------+-------------+---------------------+------------+ | ID | Name | CountryCode | District | Population | +------+------+-------------+---------------------+------------+ | 29 | Ede | NLD | Gelderland | 101574 | | 362 | Itu | BRA | São Paulo | 132736 | | 396 | Jaú | BRA | São Paulo | 109965 | | 454 | Poá | BRA | São Paulo | 89236 | | 1387 | Qom | IRN | Qom | 777677 | ................................................................ | 2902 | Ica | PER | Ica | 194820 | | 3134 | Ulm | DEU | Baden-Württemberg | 116103 | | 3379 | Van | TUR | Van | 219319 | | 3588 | Ufa | RUS | Baškortostan | 1091200 | | 3775 | Hue | VNM | Thua Thien-Hue | 219149 | +------+------+-------------+---------------------+------------+ 31 rows in set (0.00 sec) MariaDB [world]> SELECT * FROM city WHERE CHAR_LENGTH(Name)=3; //與以上結果相同。
關于“Mariadb的單表查詢方法”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Mariadb的單表查詢方法”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。