MySQL查询语句 (初入)
查询所有:
select * from product;
select * from 表单名;
select pid,pname,price,category_id from product;
select 列名1,列名2,… from 表单名;
两者作用相同,但逐个列出列名可自定义显示顺序与显示列名,如下:
查询所需数据:
select pname,price from product;
select 列名1,列名2,… from 表单名;
与全列名查询所有类似,按所需排列出列名查询所需数据
按条件查询数据:
select * from product where price > 500;
select * from product
where category_id = ‘c001’ or category_id = ‘c002’;
select 所需数据1,所需数据2,… from 表单 where 条件;
常用条件符号:大于 > ;小于 < ;等于 = ;不等于 <> 、!= 、not ; 对于数据为空的判断 is (not) null
查询聚合函数处理后的数据:
select count(pid) from product;
select count(category_id) from product;
select max(price) from product;
select min(price) from product;
select sum(price) from product;
select avg(price) from product;
select 函数(目标列 / *) from 表单名;
聚合函数:计数 count ;求最大 小 值 max min ;求和 sum ; 求均值 avg
所有聚合函数对数据进行操作时都会忽略null值项
分组查询:
select category_id,count(*) from product group by category_id;
select price,count() from product group by price having count() > 500;
select 目标列名 , count(*) from 表单名 group by 目标列名 having (被展示出的) 条件;
分页查询:
select * from product limit 0,3;
select * from product limit 3,3;
select * from product limit 6,3;
select * from product limit 9,3;
select * from product limit 12,3;
select 列名 from 表单名 limit 起始索引值,每页显示数;