|
查询数据记录,就是指从数据库对象中获取所要求的数据记录。mysql中提供了各种不同方式的数据查询方法。 一、简单数据记录查询 ?
select field1,field2,...,fieldn from t_name
*查询所有字段数据select * from t_name;
*查询指定字段数据select field1,...,fieldn from t_name;如果指定field1,...,fieldn为所有的列就成了查询所有字段了。
*避免重复数据查询--distinctselect 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.3select 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 value2select field1,field2,...,fieldn from t_name where field not between value1 and value2
select product from t_product where price between 3.3 and 6select 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,...,fieldnfrom t_namewhere conditionorder 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,...,fieldnfrom t_namewhere conditionlimit offser_start,row_count功能:
*不指定初始位置方式不指定初始位置,默认为0,表示从第一条记录开始显示.limit row_countselect 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还是非nullcount(field):实现对指定字段的记录进行统计,在具体统计时将忽略null值
2、统计计算平均值av |