这篇文章主要介绍了Mariadb的单表查询方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mariadb的单表查询方法文章都会有所收获,下面我们一起来看看吧。

MariaDB Server 是最流行的开源关系型数据库之一。它由 MySQL 的原始开发者制作,并保证保持开源。它是大多数云产品的一部分,也是大多数Linux发行版的默认配置。MariaDB 被设计为 MySQL 的直接替代产品,具有更多功能,新的存储引擎,更少的错误和更好的性能。

单表简单查询

前几天记了下创建、删除、修改数据库,表啊之类的学习笔记,今天终于要开始查询了,查询数据嘛~在我心里反正挺难的,毕竟SQL不好写,脑袋笨啊。

首先呢,Mysql官方提供了一个数据库实例给我们用,那~就是大名鼎鼎的world.sql.

进入数据库后,输入SOURCE /PATH/world.sql
例如:

MariaDB[world]>SOURCE/root/world.sqlMariaDB[world]>SHOWDATABASES;//可以看到我们的world库了+--------------------+|Database|+--------------------+|information_schema||mydb||mysql||performance_schema||world|+--------------------+MariaDB[world]>USEworld;//切换数据库DatabasechangedMariaDB[world]>SHOWTABLES;//有三张表供我们使用+-----------------+|Tables_in_world|+-----------------+|city||country||countrylanguage|+-----------------+3rowsinset(0.00sec)MariaDB[world]>DESCcity;//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...][FROMtable_references[WHEREwhere_condition][GROUPBY{col_name|expr|position}[ASC|DESC],...[WITHROLLUP]][HAVINGwhere_condition][ORDERBY{col_name|expr|position}[ASC|DESC],...][LIMIT{[offset,]row_count|row_countOFFSEToffset}][PROCEDUREprocedure_name(argument_list)][INTOOUTFILE'file_name'[CHARACTERSETcharset_name][export_options]

其中select_expr可以为其下值:

列的名称
*->所有列
Mysql函数和各种操作符
tbl_name.* 引用其他表的列
1.查询所有列

这个就没什么说的了,非常非常简单~

MariaDB[world]>SELECT*FROMcity;2.查询指定列并将其列显示为别名

别名是个很有用的功能呢~特别是多表查询的时候

MariaDB[world]>SELECTIDas'iid'FROMcityLIMIT2;+-----+|iid|+-----+|129||1|+-----+3.限定显示行数-LIMIT

之前也有用到LIMIT,LIMIT 关键字接收两个参数,第一个参数是偏移位置,第二个参数是显示行数

MariaDB[world]>SELECT*FROMcityLIMIT2,2;//这里默认是按主键排序的,所以2表示显示第3行数据,一共显示两行+----+----------------+-------------+----------+------------+|ID|Name|CountryCode|District|Population|+----+----------------+-------------+----------+------------+|3|Herat|AFG|Herat|186800||4|Mazar-e-Sharif|AFG|Balkh|127800|+----+----------------+-------------+----------+------------+2rowsinset(0.01sec)4.按照指定列排序排序数据后显示

ODER BY之前也有用到,根据指定列排序嘛~ ASC是顺序显示(默认,从小到大),DESC是倒序显示,当指定了多个列时,先按前面的列排序(分了一组),然后再在组内按后面的列排序,依次类推。以下就显示了人口最多的两个国家:

MariaDB[world]>SELECT*FROMcityORDERBYPopulationDESCLIMIT2;+------+-----------------+-------------+-------------+------------+|ID|Name|CountryCode|District|Population|+------+-----------------+-------------+-------------+------------+|1024|Mumbai(Bombay)|IND|Maharashtra|10500000||2331|Seoul|KOR|Seoul|9981619|+------+-----------------+-------------+-------------+------------+2rowsinset(0.01sec)5.去除重复的的数据

SELECTDISTINCT列名FROM表名;SELECTDISTINCT*FROMcity;//当然,这个city表中是没有重复数据的

那我们创建一个表测试一下吧:

MariaDB[world]>CREATETABLEtest(nameVARCHAR(50),passVARCHAR(50));MariaDB[world]>INSERTINTOtestVALUES//插入了4条测试数据->('test','123'),->('test','321'),->('test','123'),->('test1','123');MariaDB[world]>SELECTDISTINCT*FROMtest;+-------+------+|name|pass|+-------+------+|test|123||test|321||test1|123|+-------+------+MariaDB[world]>SELECTDISTINCTname,passFROMtest;+-------+------+|name|pass|+-------+------+|test|123||test|321||test1|123|MariaDB[world]>SELECTDISTINCTpass,nameFROMtest;+------+-------+|pass|name|+------+-------+|123|test||321|test||123|test1|+------+-------+

所以,完全相同的行才会被当作重复数据排除掉。

6.使用常量、表达式、函数进行排序

MariaDB[world]>SELECT'TheCityInfo:',//显示一列常量,当然也可是变量Name,//普通的一列CountryCodeASCountry,//使用别名Population*100ASPopulation,//对人口放大100倍MD5(ID)ASID//使用MD5函数生成ID这一列的MD5值FROMcityORDERBYPopulationDESCLIMIT2;+----------------+-----------------+---------+------------+----------------------------------+|TheCityInfo:|Name|Country|Population|ID|+----------------+-----------------+---------+------------+----------------------------------+|TheCityInfo:|Mumbai(Bombay)|IND|1050000000|021bbc7ee20b71134d53e20206bd6feb||TheCityInfo:|Seoul|KOR|998161900|273448411df1962cba1db6c05b3213c9|+----------------+-----------------+---------+------------+----------------------------------+2rowsinset(0.01sec)

使用WHERE子句过滤结果集

上面的内容,我们只是控制了显示哪些列,而并没有控制显示哪些行,当然,LIMIT可能算是控制了显示哪些行;但经常我们会有更复杂的要求,比如查找所有人口大于1,000,000的城市,这时候就可以使用WHERE子句来控制显示哪些行了。

当然,这些过滤动作可以放在业务层来做,可是,我们费了很大的力气传输了数万条数据却只用到其中的一条,有必要嘛?而且,谁能保证业务层的过滤算法能比数据库管理系统中的更高效呢?所以~一般很少有人在业务层进行数据过滤。

[WHERE where_condition]

使用WHERE子句仅需后面跟上where_condition即可,where_condition包括操作符、函数等,只要这行数据使这个表达式为TRUE则显示此行:

常用操作符:
大于: >;
小于:
等于: =;
大于等于: >=;
小于等于:
不等于: !=;
1.查询人口超过1000000的城市

MariaDB[world]>SELECT*FROMcityWHEREPopulation>=1000000;+------+--------------------------+-------------+----------------------+------------+|ID|Name|CountryCode|District|Population|+------+--------------------------+-------------+----------------------+------------+|1|Kabul|AFG|Kabol|1780000||35|Alger|DZA|Alger|2168000||56|Luanda|AGO|Luanda|2022000||69|BuenosAires|ARG|DistritoFederal|2982146|................................人工省略.............................................|3798|Phoenix|USA|Arizona|1321045||3799|SanDiego|USA|California|1223400||3800|Dallas|USA|Texas|1188580||3801|SanAntonio|USA|Texas|1144646||4068|Harare|ZWE|Harare|1410000|+------+--------------------------+-------------+----------------------+------------+238rowsinset(0.09sec)//可以看到,有238个城市的人口大于10000002.判空操作->IS NULL

当给定列的值为NULL时返回TRUE,否则返回FALSE

MariaDB[world]>SELECT*FROMcityWHERECountryCodeISNULL;Emptyset(0.00sec)MariaDB[world]>SELECTNULLISNULL;//仅当给定值为NULL时,返回TRUE+--------------+|NULLISNULL|+--------------+|1|+--------------+1rowinset(0.01sec)3.逻辑与AND操作符

表达式(测试条件)1 AND 表达式(测试条件)2 : 当表达式1和表达式2都为TRUE时,整个表达式才成立

查询中国所有人口大于100000的城市:

MariaDB[world]>SELECT*FROMcityWHERECountryCode='CHN'ANDPopulation>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|+------+---------------------+-------------+----------------+------------+341rowsinset(0.00sec)//当一条数据同时满足这两个条件才会被显示出来4.逻辑或操作符OR

表达式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|TongXian|CHN|Peking|97168|+------+-----------+-------------+----------+------------+3rowsinset(0.00sec)5.逻辑与或连用

当条件过多时,可以结合的使用与、或操作,比如:

查询北京或浙江人口大于1000000的城市:

MariaDB[world]>SELECT*FROMcityWHEREDistrict='Peking'ORDistrict='Zhejiang'ANDPopulation>1000000;+------+-----------+-------------+----------+------------+|ID|Name|CountryCode|District|Population|+------+-----------+-------------+----------+------------+|1891|Peking|CHN|Peking|7472000||1905|Hangzhou|CHN|Zhejiang|2190500||1915|Ningbo|CHN|Zhejiang|1371200||2236|TongXian|CHN|Peking|97168|+------+-----------+-------------+----------+------------+4rowsinset(0.00sec)//逻辑操作是从左向右的二目操作符,所以首先判断是District='Peking'ORDistrict='Zhejiang'这个条件,当这个条件判断完后得到的TRUE或FALSE,再用这个布尔值与ANDPopulation>1000000进行与操作。

当联合使用AND和OR时还是比较推荐使用()的,这样不容易乱。

查询浙江人口小于100000且大于10000的城市:

MariaDB[world]>SELECT*FROMcityWHEREDistrict='Zhejiang'AND(Population>10000ANDPopulationinset(0.00sec)//虽然这个例子看起来没必要使用()吧....干笑....

今天突然翻文档发现一个东西,AND的优先级要比OR优先级高,所以,看如下例子:

查询浙江或北京人口大于100000的城市:

MariaDB[world]>SELECT*FROMcityWHEREDistrict='Peking'ORDistrict='Zhejiang'ANDPopulation>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|TongXian|CHN|Peking|97168|//看这一行,为什么人口97168被筛选出来了呢?+------+-----------+-------------+----------+------------+16rowsinset(0.00sec)//原因是District='Zhejiang'ANDPopulation>100000为FALSE,然后再与其前的OR进行运算,而这条数据正好地区是北京。//所以想要的正确筛选数据,需要加一个括号6.范围检测BETWEEN AND

一个值满足一段连续的范围时为TRUE否则为FALSE

查询ID范围在1-10的城市:

MariaDB[world]>SELECT*FROMcityWHEREIDBETWEEN1AND10;+----+----------------+-------------+---------------+------------+|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|+----+----------------+-------------+---------------+------------+10rowsinset(0.00sec)

刚用Name BETWEEN ‘abc’ AND ‘efg’ 作为条件筛选了一下,竟然能筛选出900多行数据,不知道什么原理。

好像只匹配了第一个字符的ASCII值,这个东西实在想不明白有什么场景会把字符用上….

BETWEEN 1 AND 10 相当于 >=1 AND

7.离散范围检测IN

当一个值属于一段离散数据之中时为TRUE,例如 1 IN (2,3) 明显1不等于2不等于3,所以为FALSE

查询ID属于1,3,5,7,9的城市:

MariaDB[world]>SELECT*FROMcityWHEREIDIN(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|+----+-----------+-------------+---------------+------------+5rowsinset(0.01sec)

查询北京、浙江、河南的所有城市:

MariaDB[world]>SELECT*FROMcityWHEREDistrictIN('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|TongXian|CHN|Peking|97168||2242|Yuzhou|CHN|Henan|92889||2246|Linhai|CHN|Zhejiang|90870||2252|Huangyan|CHN|Zhejiang|89288|+------+--------------+-------------+----------+------------+36rowsinset(0.01sec)

所以,IN操作符跟OR的功能很类似,比如District IN (‘Peking’,’Zhejiang’,’Henan’)等于District = ‘Peking’ OR District = ‘Zhejiang’ OR District = ‘Henan’

8.逻辑非 NOT

将其原本的布尔值进行逻辑非操作后再判断

比如:查询ID

ID=10

MariaDB[world]>SELECT*FROMcityWHERENOTID>=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|+----+----------------+-------------+---------------+------------+9rowsinset(0.00sec)

所以,NOT可以用作以上任何的操作,比如,ID范围不在10-4000的城市:

MariaDB[world]>SELECT*FROMcityWHEREIDNOTBETWEEN10AND4000;+------+----------------------+-------------+----------------+------------+|ID|Name|CountryCode|District|Population|+------+----------------------+-------------+----------------+------------+|1|Kabul|AFG|Kabol|1780000||2|Qandahar|AFG|Qandahar|237500||3|Herat|AFG|Herat|186800|.............................手工省略.......................................|4077|Jabaliya|PSE|NorthGaza|113901||4078|Nablus|PSE|Nablus|100231||4079|Rafah|PSE|Rafah|92020|+------+----------------------+-------------+----------------+------------+88rowsinset(0.00sec)9.字符串搜索 LIKE

使用LIKE可以检索符合通配符的字符串,有如下两个字符串:

%:任意个任意字符
_:单个任意字符

搜索名称以T开头的城市:

MariaDB[world]>SELECT*FROMcityWHERENameLIKE'Y%';+------+--------------------+-------------+----------------+------------+|ID|Name|CountryCode|District|Population|+------+--------------------+-------------+----------------+------------+|126|Yerevan|ARM|Yerevan|1248700||516|York|GBR|England|104425||955|Yogyakarta|IDN|Yogyakarta|418944||1220|YamunaNagar|IND|Haryana|144346||1300|Yeotmal(Yavatmal)|IND|Maharashtra|108578||1396|Yazd|IRN|Yazd|326776|...........................手工省略......................................|3888|Yonkers|USA|NewYork|196086|+------+--------------------+-------------+----------------+------------+63rowsinset(0.00sec)

搜索名称为三个字母的城市:

MariaDB[world]>SELECT*FROMcityWHERENameLIKE'___';+------+------+-------------+---------------------+------------+|ID|Name|CountryCode|District|Population|+------+------+-------------+---------------------+------------+|29|Ede|NLD|Gelderland|101574||362|Itu|BRA|SãoPaulo|132736||396|Jaú|BRA|SãoPaulo|109965||454|Poá|BRA|SãoPaulo|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|ThuaThien-Hue|219149|+------+------+-------------+---------------------+------------+31rowsinset(0.00sec)MariaDB[world]>SELECT*FROMcityWHERECHAR_LENGTH(Name)=3;//与以上结果相同。

关于“Mariadb的单表查询方法”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Mariadb的单表查询方法”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。