12-2 mysql 查询
老师提纲
1. create database test
2. drop database test
3. create table info
(
code int primary key,
name varchar(20) not null
)
auto_increment 自增长列
foreign key(列名) references 主表名(列名) 外键关系
4. drop table info
CRUD:
1.insert into 表名(列名) values(值)
2.delete from 表名 where 条件
3.update 表名 set 列名=值 where 条件
简单查询
1.最简单查询(查所有数据)
select * from 表名; 注:* 代表所有列
select * from info
2.查询指定列
select code,name from info
3.修改结果集的列名
select code as '代号',name as '姓名' from info
4.条件查询
select * from info where code='p003'
5.多条件查询
查询info表中code为p003或者nation为n001的所有数据
select * from info where code='p003' or nation='n001'
查询info表中code为p004并且nation为n001的数据
select * from info where code='p004' and nation='n001'
6.范围查询
select * from car where price>=40 and price<=60
select * from car where price between 40 and 60
7.离散查询
查询汽车价格在(10,20,30,40,50,60)中出现的汽车信息
select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=60
select * from car where price in(10,20,30,40,50,60)
select * from car where price not in(10,20,30,40,50,60)
8.模糊查询(关键字查询)
查询car表里面名称包含奥迪的
select * from car where name like '%奥迪%' %任意n个字符
查询car中名称第二个字符为‘马’的汽车
select * from car 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
10.去重查询
select distinct brand from car
11.分页查询
一页显示10条 当前是第3页
select * from chinastates limit 20,10
一页显示m条 当前是第n页
limit (n-1)*m,m
12.聚合函数(统计函数)
select count(areacode) from chinastates #查询数据总条数
select sum(price) from car #求和
select avg(price) from car #求平均
select max(price) from car #求最大值
select min(price) from car #求最小值
13.分组查询
查询汽车表中每个系列下有多少个汽车
select brand,count(*) from car group by brand
查询汽车表中卖的汽车数量大于3的系列
select brand from car group by brand having count(*)>3
自己笔记
简单查询
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 40 and 60
离散查询
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*
12-2 mysql 查询的更多相关文章
- mysql数据库优化课程---12、mysql嵌套和链接查询
mysql数据库优化课程---12.mysql嵌套和链接查询 一.总结 一句话总结:查询user表中存在的所有班级的信息? in distinct mysql> select * from cl ...
- Mysql 查询练习
Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...
- MySQL查询今天/本周/上周/本月/上个月份的数据
MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看. 查询当前今天的数据 SELECT name,sub ...
- [转]向facebook学习,通过协程实现mysql查询的异步化
FROM : 通过协程实现mysql查询的异步化 前言 最近学习了赵海平的演讲,了解到facebook的mysql查询可以进行异步化,从而提高性能.由于facebook实现的比较早,他们不得不对php ...
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(摘录)
mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法分析总结: 话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添 ...
- MySQL查询in操作 查询结果按in集合顺序显示(转)
MySQL 查询in操作,查询结果按in集合顺序显示的实现代码,需要的朋友可以参考下. MySQL 查询in操作,查询结果按in集合顺序显示 复制代码代码如下: select * from test ...
- mysql查询练习
mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price, ...
- mysql查询一天,查询一周,查询一个月的数据【转】
转自:http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_d ...
- (转载)mysql查询一天,查询一周,查询一个月的数据
(转载)http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_ ...
- MYSQL查询操作 详细
学习目标 1 掌握select查询所有字段.指定字段的数据 2 掌握消除重复行命令distinct 3 掌握as给字段.表起别名 4 掌握条件查询where后跟比较运算符.逻辑运算符的用法 5 掌握条 ...
随机推荐
- Spark 1.1.0 编译(为了支持hbase 0.98.6)
为了支持hbase0.98.6,需要重新编译spark 1. 下载spark 1.1.0源代码,以及 scala-2.10.4的bin包. 将环境变量 SCALA_HOME 设置为 scala-2.1 ...
- python学习笔记六 面向对象相关下(基础篇)
面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以将多函数中公用的变量封装到对象中) 对象,根据模板创建的 ...
- 怎样让webservice在浏览器远程浏览时像在本地浏览一样有参数输入框
从远程客户端访问服务器上的WebService能够显示,但点击调用相关的方法时显示“只能用于来自本地计算机的请求”,这时提醒我们还需要在服务器进行相关的配置才能让其他机器正常访问该WebService ...
- KO Demo
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- AlarmManager 实现闹钟的基本功能
先上效果图 这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃) 1.MainActivity package com.example.domeref ...
- exception
SQL> -- 例外 exceptionSQL> -- 1/0SQL> ed已写入 file afiedt.buf 1 declare 2 pnum number := 0; ...
- 描述Linux运行级别0-6的各自含义
中文: 0: 关机模式 1:单用户模式<==破解root密码 2:无网络支持的多用户模式 3:有网络支持的多用户模式(文本模式,工作中最长使用的模式) 4:保留,未使用 5:有网络支持又x-wi ...
- 机器学习职位 & 面经
http://www.cnblogs.com/peileyuan/p/4775453.html <浅谈机器学习的职业发展方向> http://blog.sina.com.cn/s/blog ...
- springMVC的两种下载方式
1:通过httpServletResponse对象实现下载,觉得LOW的自行跳过 2:有人觉得既然使用的是MVC就要使用spring的方式
- android开发文档工具集(持续更新中...)
http://www.androiddevtools.cn/ android 产品->交互->视觉->开发->测试各种工具地址下载, 各种文档下载应有尽有,强烈推荐. ht ...