MySQL常用語句

2021-05-25 07:30:19 字數 3521 閱讀 6576

//and和or可以混用,and比or具有更高的優先順序,但盡量使用圓括號區分

//自動過濾重複的資料owner,關鍵字distinct

select distinct owner from pet;

//按照生日公升序排列,關鍵字order by

select name, birth from pet order by birth;

//降序排列,末尾加desc

select name, birth from pet order by birth desc;

//對種類公升序,對生日降序,desc只對它前面的birth起作用

select name, species, birth from pet order by species, birth desc;

//管理日期的函式,select curdate()/now(),

//計算寵物年齡,right(curdate(),5)http://

//mysql 中對於null的比較不能使用 !=<>等,應該使用 is null  或者is not null;

//sql模式匹配允許使用"_"匹配任何單個字元,

//而"%"匹配任意數目字元(包括零字元)。

//在 mysql中,sql的模式預設是忽略大小寫的。

//不能使用=或!=;而應使用like或not like比較操作符。

//模式匹配,尋找以 b 開頭的名字

select * from pet where name like 'b%';

//要想找出以「fy」結尾的名字:

select * from pet where name like '%fy';

//要想找出包含「w」的名字:

select * from pet where name like '%w%';

//要想找出正好包含5個字元的名字,使用"_"模式字元:(用5個"下劃線"表示)

select * from pet where name like '_____';

//擴充套件正規表示式的一些字元是:

· '.'匹配任何單個的字元。

· 字元類「[...]」匹配在方括號內的任何字元。例如,「[abc]」匹配「a」、「b」或「c」。

· 為了命名字元的範圍,使用乙個「-」。「[a-z]」匹配任何字母,而「[0-9]」匹配任何數字。

· "*" 匹配零個或多個在它前面的字元。例如,「x*」匹配任何數量的「x」字元,「[0-9]*」匹配任何數量的數字,

· 「.*」匹配任何數量的任何字元。

如果regexp模式與被測試值的任何地方匹配,模式就匹配(這不同於like模式匹配,只有與整個值匹配,模式才匹配)。

為了定位乙個模式以便它必須匹配被測試值的開始或結尾,在模式開始處使用「^」或在模式的結尾用「$」。

為了說明擴充套件正規表示式如何工作,下面使用regexp重寫上面所示的like查詢:

//使用正規表示式查詢---------------------------//

//查詢以b開頭的(不區分大小寫)

select * from pet where name regexp '^b';

//只查以小b開頭的

select * from pet where name regexp binary '^b';

//查詢以fy結尾的

select * from pet where name regexp 'fy$';

//為了找出包含乙個「w」的名字,使用以下查詢:

select * from pet where name regexp 'w';

//為了找出包含正好5個字元的名字,使用「^」和「$」匹配名字的開始和結尾,和5個「.」例項在兩者之間:

select * from pet where name regexp '^.....$';

//你也可以使用「」「重複n次」操作符重寫前面的查詢:

select * from pet where name regexp '^.$';

//計數行count(*)

select count(*) from pet;

//查詢每個owner有幾個pet,(必須有group by)

select owner, count(*) from pet group by owner;

//查詢那些產仔的寵物的年齡,從pet表和event表中讀取

select pet.name,(year(date)-year(birth)) - (right(date,5)

//可以針對同一表 進行聯結操作,如選出 pet表中能進行配對的動物的名字(species相同,***不同)

select p1.name, p1.***, p2.name, p2.***, p1.species from pet as p1, pet as p2

where p1.species = p2.species and p1.*** = 'f' and p2.*** = 'm';

//批處理模式下使用mysql...從略

//擁有某個列的最大值的行

//任務:找出最貴物品的編號、銷售商和**。

select article, dealer, price from  shop where  price=(select max(price) from shop);

//另乙個解決方案是按**降序排序所有行並用mysql特定limit子句只得到第一行:

select article, dealer, pricefrom shop order by price desc limit 1;

//選出每種商品的最**格

select article, max(price) as price from shop group by article

//選出每種商品**最高的dealer

select * from shop s1 where s1.price=(select max(s2.price) from shop s2 where s1.article = s2.article);

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

php mysql 常用語句 mysql常用語句

一 修改mysql使用者密碼 mysql h localhost u root p 命令列登入 update user set password password 123456 where user root 二 資料庫操作 show databases 顯示資料庫 create database ...

MySQL常用語句

1.insert into mis users name,age values ywm 13 select id,name,age from mis users limit 0,50 delete from mis users where id 8 update mis users set name...