日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL学习笔记_6_SQL语言的设计与编写(下)

發(fā)布時間:2025/3/17 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL学习笔记_6_SQL语言的设计与编写(下) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SQL語言的設計與編寫(下)

--SELECT查詢精講



概要:

SELECT[ALL | DISTINCT] #distinct 明顯的,清楚的,有區(qū)別的

{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]} #alias 別名,化名

FROM 表名

[WHERE...]

[GROUPBY...]

[HAVING...]

[ORDERBY ...]

[LIMITcount]

使用SELECT查詢語言,目的是可以按用戶的想法將數(shù)據(jù)查出來,將結果返回!


1、字段要列出要查詢的字段

e.g. selectname,price from products;

selectprice,name from products;

select* from products;

selectproducts.* from products; #單表其實不需要使用表名


2、可以為每個字段起個別名【后面會用到(關鍵字,多表查詢)】【表也可起別名(多表查詢)】

e.g. selectname as bookname,price as bookprice from products;#使用別名;也可不加as;注意別名中有空格時,需要加單引號;


3、使用distinct作用與整個記錄,取消重復的數(shù)據(jù),只返回一個,而不是單獨的一列

e.g. selectdistinct price 'book price' from products;


4、在SQL語句中使用表達式的列(可以使用算術運算符,條件運算符,邏輯運算符...

e.g. select1+2*3;

select8%5

updateproducts set num = num + 1 where id = 22;

selectname,price,price*0.7 as 'discount price' from products where id <=15;

5WHERE可以在SELECT/UPDATE/DELETE

a)可使用的邏輯運算符號(將多個條件組合)

&&/AND ||/OR !/NOT

b)可使用的比較運算符號

=#判斷是否相等,與程序中的==作用相同

<=>#判斷是否相等,與=一致,但可以用于與NULL比較

!=/ <> #不等號

<

<=

>

>=

c)程序中沒有的運算符

ISNULL #'<=>NULL' 相等

ISNOT NULL

BETWEENAND

e.g. select* from products where id between 10 and 20;

“select* from products where id >= 10 && id <= 20;”作用相同

NOTBETWEEN AND

IN

e.g. select* from products where id in(5,10,15,20);

updateproducts set num = 77 where id in(5,10,15,20);

deletefrom products where id in(5,10);


d)模糊查詢

LIKE _(任意一個字符)和%0個或多個任意字符)兩個通配符號

e.g. select* from products where name like '______'; #查找任意名字為6個字符的數(shù)據(jù)

select* from products where name like '%java%'; #查詢名字中包含有java的數(shù)據(jù)


NOTLIKE

e.g. select* from products where name not like '%java%'; #查詢名字中不包含java字樣的數(shù)據(jù)。


REGEXP/RLIKE【正則表達式】 #RegExp 正則表達式

e.g. select* from products where name regexp '^java'; #查找所有以java開頭的數(shù)據(jù)

select* from products where name regexp 's$'; #查找所有以s結尾的數(shù)據(jù)


6、多表查詢(連接查詢),比較常用 #ambiguous

e.g. selectcats.name,products.name from cats,products;

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p;#A表中的記錄與B表中的記錄依次匹配,得到A*B種結果【笛卡爾乘積】,該結果是沒有意義的。

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid;

selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid and c.id=3;

selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,catsb; #將單表分為多表,進行查詢

selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b wherea.pid = b.id;


7、嵌套查詢子查詢

e.g. select* from products where cid in(select id from carts where name regexp'^j') ;

select* from products where cid in(select id from carts where name like'j%'); #作用相同


8、orderby 字段 [asc正序]desc倒序

e.g. select* from order by name;

select* from order by price; #按價格非遞減排序

select* from order by price desc; #非遞增排序

select* from where cid > 5 order by price desc; #where結合使用

9、limitcount【限制顯示個數(shù)】

e.g. select* from limit 7;

select* from order by id desc limit 7;

select* from where id < 10 order by id desc limit 7;

select* from where id > 14 order by id asc limit 0,1; #limit0,1表示從第0個開始取,取1


10、groupby 字段【分組】

常用函數(shù):

count() #一個字段的總數(shù)

sum()

avg()#平均值

max()

min()

e.g. selectcount(*),sum(price),avg(price),max(price),min(price) from products;

selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid;

selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid having avg(price) > 50; #having條件,與where類似

#having必須與gropby結合才能使用

總結

以上是生活随笔為你收集整理的MySQL学习笔记_6_SQL语言的设计与编写(下)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。