老师提纲

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 查询的更多相关文章

  1. mysql数据库优化课程---12、mysql嵌套和链接查询

    mysql数据库优化课程---12.mysql嵌套和链接查询 一.总结 一句话总结:查询user表中存在的所有班级的信息? in distinct mysql> select * from cl ...

  2. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  3. MySQL查询今天/本周/上周/本月/上个月份的数据

    MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看. 查询当前今天的数据 SELECT name,sub ...

  4. [转]向facebook学习,通过协程实现mysql查询的异步化

    FROM : 通过协程实现mysql查询的异步化 前言 最近学习了赵海平的演讲,了解到facebook的mysql查询可以进行异步化,从而提高性能.由于facebook实现的比较早,他们不得不对php ...

  5. mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(摘录)

    mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法分析总结: 话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添 ...

  6. MySQL查询in操作 查询结果按in集合顺序显示(转)

    MySQL 查询in操作,查询结果按in集合顺序显示的实现代码,需要的朋友可以参考下. MySQL 查询in操作,查询结果按in集合顺序显示 复制代码代码如下: select * from test ...

  7. mysql查询练习

    mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price, ...

  8. mysql查询一天,查询一周,查询一个月的数据【转】

    转自:http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_d ...

  9. (转载)mysql查询一天,查询一周,查询一个月的数据

    (转载)http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_ ...

  10. MYSQL查询操作 详细

    学习目标 1 掌握select查询所有字段.指定字段的数据 2 掌握消除重复行命令distinct 3 掌握as给字段.表起别名 4 掌握条件查询where后跟比较运算符.逻辑运算符的用法 5 掌握条 ...

随机推荐

  1. python学习之if语句

    1.if条件表达式判断 ##判断条件是true or false var1=10 if var1: print("true") print(var1) else: print(&q ...

  2. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...

  3. [问题2014S02] 复旦高等代数II(13级)每周一题(第二教学周)

    问题2014S02  设实系数多项式 \begin{eqnarray*}f(x) &=& a_nx^n+a_{n-1}x^{n-1}+\cdots+a_1x+a_0, \\ g(x) ...

  4. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序实现继承

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十一篇:为ASP.NET MVC应用程 ...

  5. CSS-浮动篇float

    Float是一个强大的属性,但是它也会困扰我们如果我们不知道它的工作原理的话.这篇文章主要介绍float的原理和基本用法. 我们可以看到float在印刷世界的应用-杂志.很多杂志文章都是左边一个图片, ...

  6. HDU-4534 郑厂长系列故事——新闻净化 AC自动机+DP

    题意:给定一些单词,这些单词必须要是一个目标串的子串,同时给定一些串,这些串不能够出现在目标串中,其余一些串只会带来不同附加值.现在问满足前两者的情况下,要求附加值最大.数据给定一个原始串,现在要求在 ...

  7. [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)

    前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...

  8. shell脚本操作mysql数据库

    shell脚本操作mysql数据库,使用mysql的-e参数可以执行各种sql的(创建,删除,增,删,改.查)等各种操作 mysql  -hhostname -Pport -uusername -pp ...

  9. mysqlbinlog flashback 5.6完全使用手册与原理

    版本更新 2016/3/7 首次发布      2016/3/9  修复update无效问题      2016/4/8  修复带有秒级别以下的日期数据bug 下载地址 http://pan.baid ...

  10. centos 安装php5.6

    检查当前安装包 yum list installed | grep php 删除当前安装包 yum remove php.i686 php-bcmath.i686 php-cli.i686 php-c ...