mysql学习笔记之8(单表数据记录查询)

mysql学习笔记之八(单表数据记录查询)

查询数据记录,就是指从数据库对象中获取所要求的数据记录。mysql中提供了各种不同方式的数据查询方法。


一、简单数据记录查询


select field1,field2,...,fieldn from t_name

*查询所有字段数据
select * from t_name;

*查询指定字段数据
select field1,...,fieldn from t_name;
如果指定field1,...,fieldn为所有的列就成了查询所有字段了。

*避免重复数据查询--distinct
select distinct field1,...,fieldn from t_name

*实现数学四则运算数据查询
+ - * / %
select price,price*10 from t_product


*设置显示格式数据查询
select concat('每公斤',product,'的价格',price*2) per from t_product

二、条件数据记录查询


select field1,field2,...,fieldn from t_name where condition
功能:
*带有关系运算符和逻辑运算符的条件数据查询
> < = <= >= !=(<>) 
&&(AND) ||(OR) !(NOT) XOR(逻辑异或)


select product from t_product where price<4.0 && price>3.3
select price from t_product where product='pear';
select price from t_product where PRODUCT='pear';
两句的查询结果一样,也就是说,在这里大小写忽略了。
alter table t_product add PRODUCT float(8,2);
ERROR:Duplicate column name 'PRODUCT'

*带有between and 关键字的条件数据查询
select field1,field2,...,fieldn from t_name where field between value1 and value2
select field1,field2,...,fieldn from t_name where field not between value1 and value2

select product from t_product where price between 3.3 and 6
select product from t_product where price not between 3.3 and 6

*带is null关键字的条件数据查询
select field1,...,fieldn from t_name where field is null;
select field1,...,fieldn from t_name where field is not null;

*带in关键字的条件数据查询
判断字段的数值是否在指定集合中的条件查询。
select field1,...,fieldn from t_name where field in (value1,...,valuen);
select product from t_product where price in (43.40,6.5,8.9);
select product from t_product where price not in (43.40,6.5,8.9);


注意:
在具体使用关键字in时,查询的集合中如果存在null,则不会影响查询;如果使用关键字not in,查询的集合中如果存在null,则不会有任何的查询结果


*带like关键字的模糊数据查询
select field1,...,fieldn from t_name where field like value;
select filed1,...,fieldn from t_name where field not like value;
select field1,...,fieldn from t_name where not field like value;
like关键字支持的通配符:
-:该通配符值能匹配单个字符
%:该通配符值可以匹配任意长度的字符串。
mysql不仅对于关键字不区分大小写,对于字段数据记录也不区分大小写。
select * from t_product where product like 'a%'
select * from t_product where product like 'A%'
两句的查询结果一样
select * from t_product where product like '_d%';
select * from t_product where !(product like '_d%');


三、排序数据记录查询
select field1,field2,...,fieldn
from t_name
where condition 
order by fieldm1 [asc|desc][,fieldm2 [asc|desc],]
功能:
*按照单字段排序
关键字order by后面只接一个字段,查询结果在显示时将按照该字段进行排序。
在mysql中,null值为最小值,因此升序时将最先显示
1、升序排序
select * from t_product order by price [asc]
默认为升序
2、降序排序
select * from t_product order by price desc;
*按照多字段排序
select * from t_product order by price asc,id desc;
四、限制数据记录查询数量
select field1,...,fieldn 
from t_name
where condition
limit offser_start,row_count
功能:
*不指定初始位置方式
不指定初始位置,默认为0,表示从第一条记录开始显示.
limit row_count
select product from t_product limit 5;
注意:如果row_count大于查询的记录数,则显示所有的查询记录数;如果row_count小于查询记录数,则显示row_count条记录。

*指定初始位置方式
limit关键字经常被应用在分页系统中,对于第一页数据记录,可以通过不指定初始位置来实现,但是对于第二页等其他页面则必须指定初始位置(offset_start),否则将无法实现分页功能。除此之外,limit关键字还经常跟order by一起使用,即先对查询结果进行排序,然后显示其中部分数据记录。

select product from t_product limit 4,5;
从第五条记录开始,显示五条记录。


五、统计函数和分组数据记录查询
count(),avg(),sum(),max(),min()
在具体应用中,统计函数经常跟分组一起使用。
注意:虽然数据值没有重复也可以进行分组,但是不建议使用,因为一条数据记录也可以分成一组,但是没有任何实际意义
select function(field) 
from t_name
[where condition]
1、统计数据记录条数
count(*):实现对表中数据记录进行统计,不管表字段中包含的是null还是非null
count(field):实现对指定字段的记录进行统计,在具体统计时将忽略null值

2、统计计算平均值
avg(field):实现对指定字段的平均值进行计算,在具体统计时将忽略null值。
3、统计计算求和
sum(field):实现计算指定字段值之和,在统计时忽略null值

4、统计最大值和最小值
max(field)和min(field)
       用来实现统计数据计算求最大值和最小值,这些函数可以用来计算指定字段中的最大值和最下值或符合特定条件的指定字段值中对的最大值和最小值。
注意:
关于mysql所支持的统计数,如果所操作的表中没有任何数据记录,则count()函数会返回数据0,而其他函数则会返回null
六、分组数据函数
1、简单分组查询
在具体使用统计函数时,都是针对表中所有记录数或指定特定条件(where子句)的数据记录进行统计计算。在现实应用中,经常会把所有的数据记录进行分组,然后才对这些分组后的数据记录进行统计计算。
select function()
from t_name
where condition
group by field;
注意:在具体分组查询时,分组所依据的字段上的值一定要具有重复值,否则将没有任何实际意义。



2、实现统计功能分组查询
        mysql如果只实现简单的分组查询,是没有任何实际意义的,因为关键字group by单独使用时,默认查询出每个分组中随机一条记录,具有很大的不确定性。
        group_concat():实现显示每一个分组中的指定字段值。
select group_concat(field)
from t_name
where condition
group by field
例:
select price,count(price) as count,group_concat(product)
from t_product
group by price;
3、实现多个字段分组查询
select group_concat(field),function(field)
from t_name
where condition
group by field1,...,fieldn;
上述语句中首先会按照字段field1进行分组,然后针对每组按照field2进行分组,一次类推。
4、实现having子句限定分组查询
如果想实现对分组进行条件限制,决不能通过关键字where来实现。因为该关键字主要用来实现条件限制数据记录。因此也就有了having来实现条件限制分组数据记录。
select function(field)
from t_name
where condition
group by field1,...,fieldn
having condition;