12-4mysql 查询
简单查询
select * from 表名; 注意:*代表所有
);
查询指定列
select 列名,列名 from 表名
修改结果集的列名
select 列名 as'',列名 as'' from 表名
条件查询
select * from 表名 where 条件
多条件查询
select * from 表名 where 条件 or 条件
select * from 表名 where 条件 and 条件
范围查询
select * from 表名 where price>=40 and price<=60;
select * from 表名 where price betwen 60 and 80
离散查询
select * from 表名 where price in(20,30,40,50);
select * from 表名 where price not in(20,30,40,50)
模糊查询(关键字查询)
select * from 表名 where name like '%奥迪%' %代表任意多个字符
select * from 表名 where name like '_马%' _代表任意一个字符
9.排序查询
select * from car order by price asc asc升序(省略)
select * from car order by oil desc desc降序
先按照brand升序排,再按照price降序排
select * from car order by brand,price desc
去重查询
select distinct 列 from 表名
分页查询
一页显示10条,当前是第二页
select *from 表名 limit 10(跳过多少条),10(取第三条)
聚合函数(统计函数)
select count (主键) from 表名 查询数据总条数
select sum (列名) from 表名 求和
select avg(列名) from 表名 求平均
select max(列名) from 表名 求最大值
select min (列名) from 表名 求最小值
分组查询
查询汽车表中每个系列下有多少个汽车
select brand,count (*) from car group by brand
查询汽车表中所买的数量大于3的系列
select brand from car group by brand having count(*)>3
高级查询
1.连接查询,对结果集列的扩展
select *from 表1, 表2 #形成笛卡尔积
select *from 表1, 表2 where 表1.列=表2.列
select *from 表1join 表2 on 表1.列=表2.列
联合查询,对结果集行的扩张
select 列 from 表1
union
select 列 from 表2
子查询(里层查询)
父查询(外层查询)
子查询的结果作为父查询的条件
(1)无关子查询
子查询在执行时候和父查询没关系,子查询可以单独查询
1.查询民族为汉族的所有人员信息
父查询:select * from info where nation=()
子查询:select code from nation where name='汉族'
select * from info where nation=(select code from nation where name='汉族')
(2)相关子查询
子查询在执行时候和父查询有关系,子查询不可以单独查询
查询汽车表中油耗小于该系列平均油耗的汽车信息
父查询select *from car where oil<(该系列平均油耗)
子查询 select avg (oil) from car where brand=该系列
select *from car as a where oil<(select avg (oil) from car as b where b.brand=a.brand)
12-4mysql 查询的更多相关文章
- SQL 必知必会·笔记<12>组合查询
什么是组合查询 SQL 通过执行多个查询(多条SELECT 语句),并将结果作为一个查询结果集返回.这些组合查询通常称为并(union)或复合查询(compound query). 什么时候使用组合查 ...
- MongoDB(12)- 查询嵌入文档的数组
插入测试数据 db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A" ...
- jdbc 12: 模糊查询
jdbc连接mysql,进行模糊查询 package com.examples.jdbc.o11_模糊查询; import com.examples.jdbc.utils.DBUtils; impor ...
- 12 Mapping查询
查看 某个index下所有type的mapping GET /beauties/_mapping 查看 指定index.指定type的mapping GET /beauties/_mapping/cn
- mysql语句查询练习
1.创建students表mysql> create table ...
- 转载:MySQL 语句大全:创建、授权、查询、修改等
本文转载>这里 一.用户创建.权限.删除 1.连接MySql操作 连接:mysql -h 主机地址 -u 用户名 -p 用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit ( ...
- Drools 查询学习
Drools 查询学习查询以 query 关键字开始,以 end 关键字结束,在 package 当中一个查询要有唯一的名称,查询的内容就是查询的条件部分,条件部分内容的写法与规则的 LHS 部分写法 ...
- TSQL查询45道题
一.设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1- ...
- Mysql 查询练习
Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...
- SQL查询语句 常用示例
SQL语言的应用 1. 找出姓李的读者姓名和所在单位. 2. 列出图书库中所有藏书的书名及出版单位. 3. 查找高等教育出版社的 所有图书及单价,结果按单价降序排序. 4. ...
随机推荐
- ExtJs
ExtJS是一种主要用于创建前端用户界面,是一个与后台技术无关的前端ajax框架. 概念 1.ExtJS可以用来开发RIA也即富客户端的AJAX应用,是一个用javascript写的,主要用 ...
- Cheatsheet: 2015 05.01 ~ 05.31
.NET .NET on Mac for the OS X n00b without Mono via Visual Studio Code Microsoft frameworks deprecat ...
- xshell 终端窗口目录显示为深蓝色的不易分辨问题
xshell更改终端窗口目录展示深蓝色的不易分辨 经常使用xshell远程连接服务器,使用ls命令,目录的颜色都是深蓝色, 如果终端窗口背景颜色是黑色的(对眼睛较好的黑色的背景色,大家一般都选择黑色背 ...
- max-width实现图片的自适应
在自适应布局中,有时候会让图片随着宽度的变化相应的放大或者缩小,或者说让图片等比缩放,但是每个图片的大小又不一样,这个时候我们就要用到max-width这个属性了. img{ max-width:10 ...
- .ipynb文件 与ipython notebook
没有安装ipython notebook 后看见.ipynb文件直接手足无措了 一.安装ipython notebook 使用命令 pip ipython [all] 为所有用户安装 ipython ...
- git reset soft,hard,mixed之区别深解
GIT reset命令,似乎让人很迷惑,以至于误解,误用.但是事实上不应该如此难以理解,只要你理解到这个命令究竟在干什么. 首先我们来看几个术语 HEAD 这是当前分支版本顶端的别名,也就是在当前分支 ...
- android应用锁之获取前台进程包名方法
通过以下方式来获取前台进程的包名: 1.android api 10-20 通过ActivityManager中getRunningTasks来获取. 2.android api 21- 22(部分没 ...
- LayoutInflater和inflate()
LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater. ...
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- ajax json 动态传值
<a href="#" onclick="getRightInfo(${v.ctid})"></a> <div id=" ...