简单的select查询

select最简单的select是:select column_name[.....n]from 表名

//查询表全部记录,每条记录应该包含全部字段信息//*代表所有的列

select*from pet;=>等价于 mysql->select name ,owner,species,sex,birth,death from pet;

//指定列的查询

mysql->select name from pet;

//为列指定别名查询

mysql->select name as'姓名'from pet ;

//为表指定别名查询

mysql-> select p.name, p.sex'性别 ' from pet as p;

//增加说明列:

mysql>select name,'This is birthday:',birth from pet;

//使用DISTINCT关键字

mysql->select distinct(owner)from pet;

mysql->select distinct owner from pet;

//查询所有的数据owner不重复(也就说只出现一次)怎么办 在后面讲解后可以解答

//函数:

count

mysql>select count(*)from pet;

mysql>select count(*)’总记录数是:‘from pet;

//怎么实现计算

where子句的查询

语法是:select conditon from table_name where searchcondition;

where子句通过条件表达式描述关系中元组的选择条件

where 子句使用的条件有:

比较运算符:> <  >=  <= $amp;

逻辑运算符: and or not

  范围运算符:between and

  列表运算符:in not in

  字符匹配:like not like

  未知值:is null is not null

//比较运算符查询

//查询宠物的种类为cat的所欲宠物

mysql>select*from pet where species='cat';

//查询宠物的出生日期大于1998-01-01

mysql>select*from pet where birth$amp;>apo;$1998-01-01;

//逻辑运算查询

//查询宠物的种类是dog并且性别为f的所有宠物

mysql->select*from pet where species='dog' and sex='f';

//查询宠物的种类是dog或者性别为f的所有宠物

mysql->select*from pet where species='dog' or sex='f'

//范围查询

//查询宠物的出生日期在1993-01-01与1998-01-01之间的所有宠物

mysql>select*from pet where birth between '1993-01-01'and ' 1998-01-01';

mysql>select*from pet where birth$amp;>apos;$1993-01-01'and birth$amp;

//列表运算符

//查询宠物的种类是cat或者是bird

mysql>select*from pet where species ('cat','bird');

//查询宠物的种类不是cat并且也不是bird

mysql>select*from pet where species not in('cat','bird');

//字符匹配查询

%通配符 代表任意多个字符

_单个字符

//H%表示查询以H开头的任意字符 也可以使用^

mysql>select*from pet where owner like 'h%';

//%y表示查询在任意位置包含字母e的所有字符串

mysql>select*from pet where owner like '%e%';

//_e%表示第二个字符为e的所有字符串

mysql>select *from pet where owner like '_e%';

//未知值:

//查询宠物死亡日期为空的所有宠物

mysql> select*from pet where death is null;

//查询已经死亡的所有宠物

mysql>select*from pet where  death is not  null;

//查询出生日期在1990-1998之间宠物并且性别为f

mysql>select *from pet where birth between'1990' and '1998' and sex='f';

//查询宠物的种类是snake或者是dog的所有宠物

mysql> select*from pet where species='dog' or species='snake';

//查询种类是cat并且sex=f 或种类dog并且出生日期大于 1990;

mysql>select*from pet where(species='cat' andsex='f')or(species='dog' and birth$amp;>apos;$1990');

//查询宠物的主人是:Gwen的所有宠物

mysql>select*from pet where owner='Gwen';

//查询宠物的主人是:Gwen并且种类是cat的所有宠物的记录

mysql>select *from pet where  owner='Gwen' and species='cat';

//查询宠物的主人是:Gwen的或者种类cat的所有宠物

mysql>select*from pet where owner='Gwen' or species='cat';

//查询宠物的出生日期大于1990 并且宠物的种类是dog或者是cat的所有宠物的记录

mysql>sekect*from pet where birth$amp;>apos;$1990' and(species='dog'or species='cat');

//查询宠物主人是Gwen或者是Harlod并且宠物的种类是dog并且性别是m的所有宠物的记录

mysql>select*from pet where (owner='gwen' or owner='Harlod') and species='dog' and sex='m';

//Order by 子句 对结果集进行排序。

//排序由升序asc默认的

//降序desc 两种

mysql>select name,birth from pet order by birth;

mysql>select name,birth from pet order by birth desc;

mysql>select name,birth from pet where species='dog' order by birth;

//group by 子句

group by 子句对某一列数据的值进行分类,形成结果集,然后在结果集的基础上进行分组。

mysql>select species from pet group by species;

//第一步执行:select species from pet;

//第二步执行:group by species ;

group by 子句通常与常用的函数联合使用

count(*)//COUNT(*)它返回检索行的数目,不论其是包含null值

----------------

count() 是个聚合函数 作用是求表的所有记录数

select count(*) from 表名 这个是查询表的所有记录数

mysql-》select count(*)from pet;

sum()

avg()

max()

//宠物中最大的年龄

mysql>select max(birth) from pet;

//宠物种类年龄最大的宠物种类

mysql>select species,max(birth)from pet group by species;

//宠物种类年龄最大的宠物名称,种类

mysql>select species ,max(birth)from pet group by species;

min()

rand

round

//查询每一种宠物的个数

mysql> select species ,count(*) as'总数'from pet group by species;

mysql>select species ,count(*) from pet group by species;

//having 子句

having 子句相当于一个用于组的where子句,它制定了组或聚合的搜索条件,having子句通常与group by 子句一起使用。

在使用having 子句定义搜索条件时,它只有与组有关,而不是单个的数据有关,规则如下:

1.如果指定了group by 子句 则 having子句的搜索条件将应用于group by 子句创建的组。

案例:查询宠物种类的个数大于2的所有宠物的种类

select  count(*) as t  from  pet group by  species having t>2;

2.如果指定了where 子句,而没有指定group by 子句,那么having 子句 的搜索条件将应用于where 子句的数据结果集。

案例:在查询的结果中筛选,出生日期大于1990

select*from pet where species='dog' having birth$amp;>apo;$1990';

理解:1、首先执行select*from pet where species='dog'

    2 、在结果集在查找出生日期大于1990

   3、 如果既没有指定where子句,又没有指定group by 子句,那么having 子句的搜索条件将应用于from 子句的输出结果集

案例:查询所有的宠物信息,并且筛选出种类为dog的所有宠物

select*from pet having species='dog';

等价于:select*from pet where speices='dog'; //执行的过程一样吗

group by 与having 的综合案例:

mysql->select  species,count(*) as cnt from pet group by species;//分组查询的结果

+----------------------------------------+

|species  |   cnt  |

+----------------------------------------+

|NULL      |      2  |

|bird         |       2 |

|cat           |      2 |

|dog         |       4|

|snake    |       1|

+---------------------------------------+

5rows in set(0.00 sec)

//having 子句是在上面的结果集中进行筛选处理 种类为bird  或者dog

mysql》 select species ,count(*) as cnt from pet group by species having species in ( 'bird','dog');

+---------+-----+

  | species | cnt |

 +---------+-----+

  | bird  |  2 |

  | dog  |  4 | 

+---------+-----+







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值