欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

与MSSQL对比学习MYSQL的心得(七)--查询

程序员文章站 2024-03-02 08:44:10
比较出大家的异同点,从而加深记忆 这一篇《与mssql对比学习mysql的心得(七)》也是一样,相同的地方略略带过,不同的地方我会给出例子,谢谢大家的支持o(∩_∩)o...

比较出大家的异同点,从而加深记忆

这一篇《与mssql对比学习mysql的心得(七)》也是一样,相同的地方略略带过,不同的地方我会给出例子,谢谢大家的支持o(∩_∩)o

这一节主要介绍mysql里的基本查询(mysql官方参考手册)

mysql中select的基本语法形式:

select 属性列表
from 表名和视图列表
[where 条件表达式]
[group by 属性名[having 条件表达式]]
[order by 属性名[asc|desc]]
[limit <offset>,row count]
 
说明:

where子句:按照“条件表达式”指定的条件进行查询。

group by子句:按照“属性名”指定的字段进行分组。

having子句:有group by才能having子句,只有满足“条件表达式”中指定的条件的才能够输出。

group by子句通常和count()、sum()等聚合函数一起使用。

order by子句:按照“属性名”指定的字段进行排序。排序方式由“asc”和“desc”两个参数指出,默认是按照“asc”来排序,即升序。

建立测试表

创建测试表

create table fruits
(
 f_id char(10) not null,
 s_id int not null,
 f_name char(255) not null,
 f_price decimal(8,2) not null,
 primary key(f_id)
)

插入测试数据

insert into fruits(f_id,s_id,f_name,f_price)
values('a1',101,'apple',5.2),
('b1',102,'blackberry',5.2),
('bs1',105,'orange',5.2),
('bs2',103,'melon',5.2),
('t1',106,'banana',5.2);

使用select语句查询f_id字段的数据

select f_id,f_name from fruits

与MSSQL对比学习MYSQL的心得(七)--查询
 
注意:mysql中sql语句是不区分大小写的,因此select和select作用是相同的

这一点跟sqlserver是一样的!

常用查询

select * from fruits
select f_id,f_name from fruits where f_price >5.1
select f_id,f_name from fruits where s_id in(101,102)
select f_id,f_name from fruits where s_id not in(101,102)
select f_id,f_name from fruits where f_price between 2 and 10
select f_id,f_name from fruits where f_price not between 2 and 10

 
带like的字符匹配查询

1、百分号通配符“%”,匹配任意长度的字符,甚至包括零字符

select f_id,f_name from fruits where f_name like 'b%y'

与MSSQL对比学习MYSQL的心得(七)--查询

 2、下划线通配符“_”,一次只能匹配任意一个字符

下面语句有四个下划线

select f_id,f_name from fruits where f_name like '____n'

与MSSQL对比学习MYSQL的心得(七)--查询

同样,在sqlserver里面也是有的

use [sss]
go
select * from [dbo].[aaa] where [name] like 's_____'


 与MSSQL对比学习MYSQL的心得(七)--查询
查询空值

create table customers
(
 c_id int not null auto_increment,
 c_name char(25) not null,
 c_city char(50)  null,
 primary key(c_id)
)
insert into customers(c_name,c_city)
values('liming','china'),
('hongfang',null)
select * from customers where c_city is null

与MSSQL对比学习MYSQL的心得(七)--查询

select * from customers where c_city is not null

与MSSQL对比学习MYSQL的心得(七)--查询

and、or、distinct关键字

select f_id,f_name from fruits where f_name like '____n' and f_id='bs2'
select f_id,f_name from fruits where f_name like '____n' or f_id='bs2'
select distinct s_id from fruits

 

 
group by

select s_id ,count(1) as total from fruits group by s_id

与MSSQL对比学习MYSQL的心得(七)--查询

再插入两条记录

insert into fruits(f_id,s_id,f_name,f_price)
values('a6',101,'cherry',6),
('a8',102,'coconut',7)

 
如果要查看每个供应商提供的水果的种类的名称,mysql中可以在group by中使用group_concat()函数,

将每个分组中各个字段的值显示出来

select s_id,group_concat(f_name) as names from fruits group by s_id 

与MSSQL对比学习MYSQL的心得(七)--查询

sqlserver是没有group_concat()函数的,sqlserver要达到同样效果需要使用xml函数,mysql这方面做得非常好

having:过滤分组

根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息

select s_id ,group_concat(f_name) as names from fruits group by s_id having count(f_name)>1

与MSSQL对比学习MYSQL的心得(七)--查询
 
在group by中使用with rollup

select s_id ,count(1) as total from fruits group by s_id with rollup

与MSSQL对比学习MYSQL的心得(七)--查询

增加了最后一行,7表示total列的所有值的总和

而rollup关键字在sqlserver里面也有,详见:sqlserver中的all、percent、cube关键字、rollup关键字和grouping函数

注意:当使用rollup时,不能同时使用order by子句进行结果排序,即rollup和order by是互相排斥的!

limit限制查询结果的数量

在sqlserver中是使用top关键字,而在mysql中是使用limit关键字

limit[位置偏移量],行数
 第一个“位置偏移量”参数指示mysql从哪一行开始显示,是一个可选参数,如果不指定“位置偏移量”

将会从表中第一条记录开始(第一条记录的位置偏移量是0,第二天记录的位置偏移量是1......以此类推)

第二个参数“行数”指示返回的记录条数

select * from fruits

与MSSQL对比学习MYSQL的心得(七)--查询

select * from fruits limit 4,3

与MSSQL对比学习MYSQL的心得(七)--查询

上面结果返回从第5条记录行(因为从0开始数)开始之后的3条记录

注意:在mysql5.6中可以使用 limit 4 offset 3 ,意思是获取从第5行记录开始的3条记录,和 limit 4,3 返回的结果是一样的

在sqlserver2012里面开始支持类似mysql的这种语法,但是需要按某一列先排序,而不像mysql那样不用按照某一列排序

use [sss]
go

create table fruits
(
  f_id char(10) not null ,
  s_id int not null ,
  f_name char(255) not null ,
  f_price decimal(8, 2) not null ,
  primary key ( f_id )
)

insert into fruits
    ( f_id ,
     s_id ,
     f_name ,
     f_price
    )
    select 'a1' ,
        101 ,
        'apple' ,
        5.2
    union all
    select 'b1' ,
        102 ,
        'blackberry' ,
        5.2
    union all
    select 'bs1' ,
        105 ,
        'orange' ,
        5.2
    union all
    select 'bs2' ,
        103 ,
        'melon' ,
        5.2
    union all
    select 't1' ,
        106 ,
        'banana' ,
        5.2
    union all
    select 'a6' ,
        101 ,
        'cherry' ,
        6
    union all
    select 'a8' ,
        102 ,
        'coconut' ,
        7
 

先按s_id排序,然后返回第5行开始的3条记录

select *
from  [dbo].[fruits]
order by [s_id] asc
    offset 4 rows
fetch next 3 rows only;

与MSSQL对比学习MYSQL的心得(七)--查询
 
虽然没有mysql那么方便,不过也算是一种进步,而对于offset fetch next的性能可以参考宋沄剑的文章:

sql server2012 t-sql对分页的增强尝试

子查询

子查询这个特性从mysql4.1开始引入。

插入测试数据

create table tbl1(num1 int not null);
create table tbl2(num2 int not null)

insert into tbl1 values(1),(4),(13),(27);
insert into tbl2 values(6),(14),(11),(20)

 
any关键字接在一个比较操作符的后面,表示若与子查询返回的任何值比较为true,则返回true

返回tbl2表的所有num2列,然后将tbl1中的num1的值与之进行比较,只要大于num2的任何一个值,即为符合查询条件的结果

select num1 from tbl1 where num1>any(select num2 from tbl2)

与MSSQL对比学习MYSQL的心得(七)--查询

all关键字接在一个比较操作符的后面,表示与子查询返回的所有值比较为true,则返回true

select num1 from tbl1 where num1>all(select num2 from tbl2)

与MSSQL对比学习MYSQL的心得(七)--查询

在sqlserver中也可以使用any和all关键字

use [sss]
go
create table tbl1(num1 int not null)
create table tbl2(num2 int not null)

insert into tbl1 values(1),(4),(13),(27)
insert into tbl2 values(6),(14),(11),(20)

select num1 from tbl1 where num1>any(select num2 from tbl2)
select num1 from tbl1 where num1>all(select num2 from tbl2)

结果是一样的

与MSSQL对比学习MYSQL的心得(七)--查询

合并查询

使用union关键字,合并结果时,两个查询对应的列数和数据类型必须相同。

各个select语句之间使用union或union all关键字分隔

union:执行的时候删除重复的记录,所有返回的行都是唯一的

union all:不删除重复行也不对结果进行自动排序

select s_id,f_name,f_price  
from fruits
where f_price<9.0
union 
select s_id,f_name,f_price
from fruits
where s_id in (101,103)

与MSSQL对比学习MYSQL的心得(七)--查询

第一个查询把f_price小于9.0的记录查询出来,第二个查询把s_id为101和103的记录查询处理

因为f_price小于9.0的记录里有些记录的s_id是102、105、106,这些结果不会被去掉会跟第二个查询进行合并

所以最终的结果会有s_id为102、105、106的记录

正则表达式查询

正则表达式在sqlserver里面是没有的,但是在mysql里不单只有,而且功能也比较丰富

mysql中使用regexp关键字指定正则表达式的字符匹配模式

1、查询以特定字符或字符串开头的记录

字符“^”匹配以特定字符或者字符串开头的文本

select * from fruits where f_name regexp '^b'

返回f_name字段以b开头的记录

与MSSQL对比学习MYSQL的心得(七)--查询

2、查询以特定字符或字符串结尾的记录

字符“$”匹配以特定字符或者字符串结尾的文本

select * from fruits where f_name regexp 'y$'

返回f_name字段以y结尾的记录

与MSSQL对比学习MYSQL的心得(七)--查询

3、用符号“.”来代替字符串中的任意一个字符

字符“.”匹配任意一个字符

select * from fruits where f_name regexp 'a.g'

与MSSQL对比学习MYSQL的心得(七)--查询

a和g两个字母之间包含单个字符,orange符合要求

4、使用“*”和“+”来匹配多个字符

星号“*”匹配前面的字符任意多次,包括0次。加号“+”匹配前面的字符至少一次

select * from fruits where f_name regexp '^ba*'

与MSSQL对比学习MYSQL的心得(七)--查询

blackberry和banana符合要求,b开头,a匹配任意多次,不管出现的顺序在哪里

select * from fruits where f_name regexp '^ba+'

与MSSQL对比学习MYSQL的心得(七)--查询

 “a+”匹配字母“a”至少一次,只有banana满足匹配条件

5、匹配指定字符串

正则表达式可以匹配指定字符串,只要这个字符串在查询文本中即可,如要匹配多个字符串,多个字符串之间使用分隔符“|”隔开

select * from fruits where f_name regexp 'on|ap'

与MSSQL对比学习MYSQL的心得(七)--查询

可以看到apple 、melon 、coconut 3个值中都包含有字符串“on”和“ap”,满足匹配条件

6、匹配指定字符中的任意一个

方括号“[]”指定一个字符集合,只匹配其中任何一个字符,即为所查找的文本

select * from fruits where f_name regexp '[ot]'

与MSSQL对比学习MYSQL的心得(七)--查询

方括号[]还可以指定数值集合

select * from fruits where s_id regexp '[456]'

与MSSQL对比学习MYSQL的心得(七)--查询

s_id字段值中有3个数字中的1个即为匹配记录字段

[456]也可以写成[4-6]即指定集合区间

7、匹配指定字符以外的字符

“[^字符集合]”匹配不在指定集合中的任何字符

select * from fruits where f_id regexp '[^a-e1-2]'

 与MSSQL对比学习MYSQL的心得(七)--查询


返回开头不在a-e  1-2字母的记录,例如a1,b1这些记录就不符合要求

8、使用{n,} 或者{n,m}来指定字符串连续出现的次数

“字符串{n,}”,表示至少匹配n次前面的字符;“字符串{n,m}”表示匹配前面的字符串不少于n次,不多于m次。

select * from fruits where f_name regexp 'b{1,}'

与MSSQL对比学习MYSQL的心得(七)--查询

至少匹配1次字母b,blackberry和banana都符合要求

 select * from fruits where f_name regexp 'ba{1,3}'

与MSSQL对比学习MYSQL的心得(七)--查询

“ba”字符串最少出现一次,最多三次,banana这个字符串符合要求

总结

这一节介绍了mysql里的查询,并且比较了与sqlserver的区别,特别是mysql里的正则查询灵活多变

这一点比sqlserver略为优胜

如有不对的地方,欢迎大家拍砖o(∩_∩)o

附上作者艳照一张o(∩_∩)o 猜猜是哪个

与MSSQL对比学习MYSQL的心得(七)--查询