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 ;求最大
小值 maxmin;求和 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 起始索引值,每頁顯示數;

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/13329.html
標籤:其他
上一篇:GROUP BY 的內在細節展示!!(不可忽略點!!)
下一篇:MySql語法及命令(用心整理)
