Mysql的DQL查詢操作全面分析講解
DQL簡介
概念:DQL(data query language)數(shù)據(jù)查詢語言 select操作
排序規(guī)則:
- select 表達式1|字段,.... - from 表名 where 條件 - group by 列名 - having 條件 - order by 列名 asc|desc - limit 位置,數(shù)量
語法結構:
SELECT [ALL | DISTINCT] ALL表示查詢出所有的內容 DISTINCT 去重
{* | 表名.* | 表名.字段名[ AS 別名][,...]} 指定查詢出的字段的
FROM
表名[AS 別名][,表1... AS 別名]
[INNER | [LEFT | RIGHT] [OUTER] JOIN 另一張表名 [AS 別名] ON 關聯(lián)條件]
[WHERE 條件]
[GROUP BY 分組字段[,...]]
[HAVING 給分組后的數(shù)據(jù)進行條件篩選]
[ORDER BY 排序字段[,...]]
[LIMIT [startIndex,]pageSize]
具體操作
數(shù)據(jù)準備
create database if not exists test; use test; create table if not exists data( id tinyint primary key auto_increment, price double NOT null, name varchar(20) not null, type varchar(20) not null) ; insert into data values (null,900,'洗衣機','b'), (null,1900,'冰箱','b'), (null,2900,'空調','b'), (null,3900,'電視','b'), (null,150,'衣服','c'), (null,180,'褲子','c'), (null,200,'鞋子','c'), (null,188,'洗面奶','a'), (null,188,'洗發(fā)水','a'), (null,199,'洗衣液','a'), (null,88,'沐浴露','a'), (null,5,'泡面','d'), (null,15,'餅干','d'), (null,30,'咖啡','d');
簡單查詢
select * from data; select name,price from data; select * from data as d; select * from data d; select d.name,d.price from data d; select distinct price from data; select name,price +100 newprice from data;
運算符
算術運算符
select name,price *1.5 newprice from data;
條件查詢
select * from data where name='洗衣機'; select * from data where !(price>100); select * from data where price between 200 and 1000; select * from data where price in(188,900); -- 等于下面兩句 select * from data where price = 188 or price =900; select * from data where price = 188 || price =900; select * from data where name like '%衣%'; select * from data where name like '衣%'; select * from data where name like '_衣%'; select * from data where id is null;
注釋:當有NULL作為比較大小的對象時,最大值和最小值均為null
排序查詢
select * from data order by price; select * from data order by price desc; select distinct price from data order by price desc; select * from data order by price,id;
聚合查詢
select count(*) from data; -- 不全為空的行數(shù) select count(id) from data; -- 通過主鍵值查詢行數(shù) select count(*) from data where price<200; select sum(price) from data where type='A'; select max(id) from data; select min(price) from data; select max(price) max_price,min(price) min_price from data; select avg(price) from data where type='c';
null值的處理
分組查詢
select sum(price) from data group by type; select type,count(id) from data group by type;
條件篩選
select type,count(id) count from data group by type having count=4 order by type;
分頁查詢
分頁顯示
select * from data limit 5; -- 從第四條開始依次向后顯示五條 select * from data limit 3,5;
insert into select語句
create table data2( name varchar(10), price double); insert into data2 select name,price from data; select * from data2; create table data3( type varchar(10), num int ); insert into data3 select type,count(*) from data group by type order by count(*); select * from data3;
總結
到此這篇關于Mysql的DQL查詢操作全面分析講解的文章就介紹到這了,更多相關Mysql DQL查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MySQL數(shù)據(jù)庫優(yōu)化之分表分庫操作實例詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫優(yōu)化之分表分庫操作,結合實例形式詳細分析了mysql數(shù)據(jù)庫分表分庫垂直拆分、水平拆分相關原理以及應用案例,需要的朋友可以參考下2020-01-01my.cnf參數(shù)配置實現(xiàn)InnoDB引擎性能優(yōu)化
目前來說:InnoDB是為Mysql處理巨大數(shù)據(jù)量時的最大性能設計。它的CPU效率可能是任何其它基于磁盤的關系數(shù)據(jù)庫引擎所不能匹敵的。在數(shù)據(jù)量大的網(wǎng)站或是應用中Innodb是倍受青睞的。另一方面,在數(shù)據(jù)庫的復制操作中Innodb也是能保證master和slave數(shù)據(jù)一致有一定的作用。2017-05-05深入解析mysql中order by與group by的順序問題
本篇文章是對mysql中order by與group by的順序問題進行了詳細的分析介紹,需要的朋友參考下2013-06-06mysql數(shù)據(jù)插入覆蓋和時間戳的問題及解決
這篇文章主要介紹了mysql數(shù)據(jù)插入覆蓋和時間戳的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03