博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库基础技巧及用法
阅读量:3888 次
发布时间:2019-05-23

本文共 1995 字,大约阅读时间需要 6 分钟。

Mysql基础应用(包括表连接)

1.去重查询:distinct

只有select语句可以使用它,而且必须放在select之后。

例子:select DISTINCT mobile,phone from js_sys_user

2.排序查询:order by

  1. 需要升序排序时,末尾加上ASC;需要降序排序时,末尾加上:DESC,默认的order by是升序排序的。
  2. Order by是可以用date类型或者datetime类型来排序的。

例子:升序:select * from js_sys_user ORDER BY pwd_update_date

降序:select * from js_sys_user ORDER BY pwd_update_date DESC

3.限制查询数:limit

select mobile,phone from js_sys_user limit 3

4.聚拢查询(分组查询)、聚拢函数:group by、Having

聚拢查询大多配合聚拢函数使用。常用的聚拢函数有:count,sum,avg,max,min

例子:select count(*),SUM(mobile),avg(mobile) from js_sys_user GROUP BY user_type

Having表示对聚拢后的内容进行再条件过滤。

例子:select count(*) from js_sys_user GROUP BY user_type HAVING count(*)>3

5.表连接

5.1内连接

例子:select email,role_code from js_sys_user jsu,js_sys_user_role jsur where jsu.user_code=jsur.user_code

5.2外连接

左连接:left  join…..on

含义:以左边的表为主表,主表的所有查询记录无论有没有和右表匹配,都会查询出来,而右表只有当它和左表匹配时才可以被查询出来。

例子:select user_name,role_code from js_sys_user jsu LEFT JOIN js_sys_user_role jsur on jsu.user_code=jsur.user_code

右连接:right  join…..on

含义:以右边的表为主表,主表的所有查询记录无论有没有和左表匹配,都会查询出来,而左表只有当它和右表匹配时才可以被查询出来。

例子:select user_name,role_code from js_sys_user jsu LEFT JOIN js_sys_user_role jsur on jsu.user_code=jsur.user_code

6.子查询:in、not in、exists、not exists

重点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Inexists的区别:

例如有查询语句:Select * from A where id in(select id from B)

-对于in来说,in是先对B表进行查询,得到查询结果后与A表做笛卡儿积进行匹配。因此当B表的数据比A表多的时候,使用in的数据库查询效率比较高。

-对于exists来说,exists是先对A表进行查询,得到查询结果后与B表做笛卡儿积进行匹配。因此当A表的数据比B表多的时候,使用exists的数据库查询效率更高。

-A、B表数据差不多的时候,两者查询效率相近。

 

7.区间查询:between…and

例子:select * from js_sys_area where area_code BETWEEN 370000 and 370111

8.空数据查询:is null

例子:select * from js_sys_user where email is null

9.模糊查询:like

左模糊查询:select * from js_sys_user where user_name like '%0'

右模糊查询:select * from js_sys_user where user_name like '用户%'(右模糊查询能够索引,不需要查询全表,因此查询效率是最高的)

全模糊查询:select * from js_sys_user where user_name like '%管理员%'

10.匹配正则查询:regexp

能够获得匹配正则的内容。

例子:select * from js_sys_user where password REGEXP 'ba'

11.或查询:or(最好不用,否则将会放弃索引采用全表查询)

转载地址:http://beshn.baihongyu.com/

你可能感兴趣的文章
theano 后端爆内存
查看>>
os.environ 和 keras.json
查看>>
后台面试经典问题-手写LRU算法
查看>>
Part-Guided Attention Learning for Vehicle Instance Retrieval
查看>>
Deep Residual Learning for Image Recognition
查看>>
Bag of Tricks and A Strong Baseline for Deep Person Re-identification
查看>>
vue+flask实现视频目标检测yolov5
查看>>
关于BigInteger
查看>>
UIScrollView不能响应UITouch事件
查看>>
iOS TextFiled 文本密码切换 光标偏移解决
查看>>
iOS 当前应用所占内存和设备可用内存
查看>>
iOS 文件属性
查看>>
UIView的layoutSubviews和drawRect方法何时调用
查看>>
iOS GCD多线程下载原理
查看>>
NSData全部API解释
查看>>
iOS 侧滑菜单封装Demo(类似QQ侧滑效果)
查看>>
Spring学习(二)
查看>>
Spring学习(三)
查看>>
Spring学习(四)
查看>>
java解惑——易错知识点归纳总结
查看>>