1. 程式人生 > 其它 >MySQL(查詢命令)

MySQL(查詢命令)

以什麼開頭以什麼結尾查詢

以什麼開頭 命令: select * from tableName where 欄位 rlike '^ch' limit 5;

以什麼結尾 命令: select * from tableName where 欄位 rlike 'yi$' limit 5;

以別名查詢

命令: select 欄位 as 別名 from tableName;

 

最大,最小,平均,總數

函式:實現某一特定的功能

max():最大

select max(欄位)from tableName;

min():最小

select min(欄位)from tableName;

avg():平均

select avg(欄位)from tableName;

總數

select sum(欄位)from tableName;

排序

命令:select * from tableName order by 欄位;

預設是sec 由低到高

order by :排序

 

倒序

select * from tableName order by 欄位 desc;

由大到小

查詢大於等於50並倒序

select * from student where score>=50 order by score desc;

去重

select distinct 欄位 from tableName;

distinc 去重

 

聚合函式:group by

having與where的區別

WHERE 與 HAVING 的根本區別在於:

WHERE 子句在 GROUP BY 分組和聚合函式 之前 對資料行進行過濾;

HAVING 子句對 GROUP BY 分組和聚合函式 之後 的資料行進行過濾。

having 是根據聚合函式 group by結合使用的

實戰:以年齡查詢學生的平均分大於50並且以平均分倒序

select age,avg(score) as 平均分 from student group by age having 平均分>50 order by 平均分 desc;

 

查詢員工裡有多少名男女

select gender as 性別,count(gender) as 人數 from employees group by gender;

 

 查詢 employees表每個last_name一樣的有多少人顯示10行

 

 

windows mysql配置檔案

查詢倆個或兩個表以上共同擁有的資料

inner join,⼜叫內連線的部分,主要是獲取兩個表中欄位匹配關係的表。查詢關聯欄位共同擁有的資料

select shop_name,good_name,good_type,price from shop inner join goods on shop.id=goods.shop_id;

 

查詢店鋪名稱,快遞名稱,快遞電話

select shop_name,name,phone from shop inner join goods on shop.id=goods.shop_id inner join logistic on goods.id=logistic.good_id;

查詢shop,goods表的所有欄位 2、以商品的價格作為倒序排序

select * from shop inner join goods on shop.id= goods.shop_id order by goods.price desc;