老师提纲

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. c++实现螺旋矩阵分析总结

    螺旋矩阵,是这么一个东西: 1   2   3 8   9   4 7   6   5 这是一个,n*n的矩阵,由外向里一次递增,一环一环,就好像一个螺旋一样.不难想象,如果n=5,那么应该是这样的: ...

  2. Oracle的排序和限制条件(order by 和where)

    1.Order by 子句的使用 select column.... from .... order by ... 1) Order by子句在整个 select语句中的位置: 始终位于最后 2) o ...

  3. C# 从CIL代码了解委托,匿名方法,Lambda 表达式和闭包本质

    前言 C# 3.0 引入了 Lambda 表达式,程序员们很快就开始习惯并爱上这种简洁并极具表达力的函数式编程特性. 本着知其然,还要知其所以然的学习态度,笔者不禁想到了几个问题. (1)匿名函数(匿 ...

  4. Git 忽略文件

    在Git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则例如: # 此为注释 – 将被 Git 忽略 *.a    ...

  5. Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法 + Linux启动/停止/重启Mysql数据库的方法

    启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...

  6. AlarmManager 实现闹钟的基本功能

    先上效果图 这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃) 1.MainActivity package com.example.domeref ...

  7. c#入门笔记(2)控件

    1.控件是可以从工具栏直接拖动到窗体,具有确定功能的函数.大部分控件属于control类的派生类.通用属性name(名称),location(具体的位置),top,left(位置) 2.form窗体类 ...

  8. Android 连接webservice(利用谷歌提供的jar包)

    Android开发,需要连接webservice,之前就想用谷歌提供的jar包,下载地址:http://pan.baidu.com/s/1hqMTUHe 把它下载下来粘贴到libs文件夹下即可: 网上 ...

  9. python的class的__str__()和__repr__()函数

    repr(object) 返回一个可以用来表示对象的可打印字符串首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象 否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地 ...

  10. Android权限声明

    1.网络 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/ > < ...