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

歡迎訪問 生活随笔!

生活随笔

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

数据库

mysql再探

發(fā)布時間:2024/4/17 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql再探 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

select子句及其順序

select from where group by having order by limit ??

創(chuàng)建表

create table student(id int not null auto_increment,name varchar(20) default 'noname',age int,primary key(id)) engine=innodb;主鍵必須唯一,而且主鍵可以為多個,例如primary(id,name),auto_increment自增,default默認值,engine引擎,引擎有innodb,可靠的事物處理引擎,不支持全文本搜索,memory 數(shù)據(jù)存儲在內(nèi)存不是磁盤,速度快,適合臨時表,myisam性能極高的引擎,支持全文本搜索,但不支持事物處理

插入數(shù)據(jù)

insert into student values(123,'linux',22);

insert into student values(123,'linux',22),(11,'test',23);同時插入兩條數(shù)據(jù)

insert into student(id ,name,age) values(123,'linux',22);

insert into student(id ,name) values(123,'linux');

insert into student(id ,name,age) select id_cp,name_cp,age_cp from student_cp;插入檢索出的數(shù)據(jù)


更新數(shù)據(jù)

update student set id=100 where name='test';

update student set id=NULL where name='test';

update student set id=100,age=22 where name='test';同時修改id和age

刪除數(shù)據(jù)

delete from student where name='test';刪除名字為test的那行

更新表

alter table student add addr char(20);給student表增加一個地址列

alter table student drop column addr;刪除student表地址列

定義外鍵

alter table orderitems add constraint fk_orderitems_orders? foreign key (order_num)? references orders (order_num); 其中constraint命名外鍵為fk-orderitems_orders,對應外鍵為order_num,鏈接的外表列為order_num

刪除表

drop table student;

重命名表

rename table student to stuinfo;重命名為stuinfo

rename table student to stuinfo,orders to dingdan;同時重命名兩個表



正則表達式的使用

???? select? * from student where name regexp 'bp' order by id;找出名字包含bp的所有行

????

??? select? * from student where name regexp 'bp.' ; 名字為bp且后面跟一個字符的學生,如果需要區(qū)分大小寫,只需要在regexp后面加關(guān)鍵字binary


??? select? * from student where name regexp 'bp|bp1|bp2';列出名字為bp或者bp1和bp2的


??? select? * from student where name regexp 'bp[12]';列出叫做bp1以及bp2的學生,改成bp[1|2]也是一樣的意思


??? select? * from student where name regexp 'bp[^12]';列出不叫做bp1以及bp2的學生


???select? * from student where name regexp 'bp[1-9]';列出叫做bp1,bp2,bp3...的學生


??? select? * from student where name regexp 'bp[a-z]';列出叫做bpa,bpb,bpc...的學生




??? 匹配特殊字符

??

???select? * from student where name regexp '\\-';列出包含字符-的學生,\\用來轉(zhuǎn)義


? ?匹配字符類


??? [:alnum:] 任意字符和數(shù)字[a-zA-Z0-9]

??? [:alpha:]? 任意字符[a-zA-Z]

??? [:blank:]? 空格和制表[\\t]

??? [:cntrl:]??? ASCII控制字符,ASCII 0-31和127

??? [:digit:]??? 任意數(shù)字[0-9]

??? [:graph:]? 與[:print:]相同,但不包括空格

??? [:lower:]?? 任意小寫字母[a-z]

??? [:upper:]?? 任意大寫字母[A-Z]

??? [:print:]???? 任意可打印字符

??? [:punct:]?? 既不在[:alnum:]又不在[:cntrl:]中的任意字符

??? [:xdigit:]??? 任意十六進制數(shù)字[a-fA-F0-o]

??? [:space:]?? 包括空格在內(nèi)的任意空白字符[\\f\\n\\r\\t\\v]


??? 匹配多個實例

???

??? *???? ? ? ?? 0個或多個匹配

? ? +?????????? 1個或多個匹配

?? ??????????? 0個或1個匹配

??? {n}???????? 指定數(shù)目匹配

??? {n,}??????? 大于或等于n次的匹配

??? {n,m}???? 匹配數(shù)目為n到m

???


??? select? * from student where name regexp '\\([0-9] bp?\\)';名字包含(1 bpa)之類的


???select? * from student where name regexp '[[:digit:]]{4}';名字包含4個數(shù)字的學生


????定位符

??

????? ^?????????????????? 文本的開始

????? $?????????????????? 文本的結(jié)束

????? [[:<:]]???????????? 詞的開始

????? [[:>:]]???????????? 詞的結(jié)束


????? select? * from student where id regexp '^[0-9\\.]';等價'^[0-9|\\.]即數(shù)字開頭或.開頭,注意區(qū)分,如果^在[ ]里面,是否定該集合的意思,在[ ]外面就是開頭的意思


????
????? select concat(id,name,age) from student;把id和name以及age連起來成為一個字符串


? ? ??select trim(name) from student;去掉name左右的空格后輸出,ltrim去掉左邊空格,rtrim去掉右邊空格


????select concat(id,name,age) as? linux from student;把列名改為linux


??? select price,counts,price*counts as money from produces;增加一行統(tǒng)計價格并命名為money


??? select upper(name) as linux from student;將名字轉(zhuǎn)換為大寫,lower()小寫,left()返回串左邊的字符,right()返回串右邊的字符,locate()找出串的一個子串,soundex()返回串的soundex值,即發(fā)音差不多的,如lee和lie等等。


??? 時間和日期處理函數(shù)

?

??? adddate()???????????????????? 增加一個日期

??? addtime()???????????????????? 增加一個時間

??? curdate()????????????????????? 返回當前時間

??? curtime()????????????????????? 返回當前時間

??? date()?????????????????????????? 返回日期時間的日期部分

??? datediff()????????????????????? 計算兩個日期之差

??? day()? hour()?? minute()?? month()??? now()?? second()?? year()?? 返回對應時間

???

??? select * from orders where date(order_date)='2017-5-1' ;如果只知道查找某一天的,但是訂單里的日期有包括了具體時刻,那么將其轉(zhuǎn)換為date格式,就能找到相應日期的訂單了

?

???select * from orders where date(order_date)='2017-5-1' and '2017-5-3';訂單在該范圍



???select * from orders where year(order_date)=2017 and month(order_date)=4;年月


??? 數(shù)值處理函數(shù)


??? abs???????? 絕對值

??? cos???????? 余弦值

??? exp???????? 指數(shù)值

??? mod??????? 余數(shù)

??? pi??????????? 圓周率

??? rand??????? 隨機數(shù)

??? sin????????? 正弦值

??? sqrt???????? 平凡根

??? tan????????? 正切值


?? 匯總數(shù)據(jù)

???

??? avg???????? 某列的平均值

??? count????? 某列的行數(shù)

??? max??????? 某列的最大值

??? min???????? 某列的最小值

??? sum??????? 某列值之和

??? select min(id) from student;? 打印最小的id


??? select sum(price*counts) as money from produces;統(tǒng)計總價

???

??? 分組數(shù)據(jù)

???

??? select id,count(*) from student group by id;根據(jù)id分組,并且統(tǒng)計每組id數(shù)據(jù)數(shù)量


??? select id,count(*) from student group by id having count(*)>=2;打印id組數(shù)量大于2的,having相當跟where差不多,唯一的差別在于where過濾行,having過濾組

???

???select id,count(*) from student where id>=2 group by id having count(*)>=2;


??? group by 和 order by 的區(qū)別:

??? order by 產(chǎn)生有序的的輸出,group by 分組行,但輸出可能不是分組的順序

???

???? select id,sum(id*age) from student group by id order by sum(id*age); 計算每個id組里面id*age的總和,再根據(jù)和排序


??? 子查詢

???

???? select cust_id from orders where order_num in(select order_num from orderitems where prod_id ='TNT2');

?

???? select cust_name,cust_state,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers order by cust_name;


???? select vend_name,prod_name,prod_price from vendors,products where vendors.vend_id=products.vend_id order by vend_name,prod_name

??

???? 笛卡兒積(搜索結(jié)果行的數(shù)目是地一個表的行數(shù)乘以第二個表的行數(shù))

???? select vend_name,prod_name,prod_price? from vendors,products order by vend_name,prod_name;


???? 內(nèi)部聯(lián)結(jié)

???? select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id;?? 內(nèi)部聯(lián)結(jié)inner join on?? 聯(lián)結(jié)條件用on來代替where

?

??? 表別名

??? select a.id,a.name from student as a; 用a來代替student

???

???? 左外連接

??? select customers.cust_id,orders.order_num from customers left outer join orders on customers.cust_id=orders.cust_id;? 右外連接是right outer join on,其區(qū)別是左連接時,如果右邊沒有數(shù)據(jù),那么只會顯示左邊的數(shù)據(jù),右邊顯示null,右外連接則相反


?? 聯(lián)合查詢

?? select vend_id,prod_id,prod_price from products where prod_price <=5?? union select vend_id,prod_id,prod_price from products where vend_id in(1001,1002);把滿足這兩個條件的都列出來,而且會去掉重復的行,如果不想去掉重復的行,使用union all,注意union必須由兩條或者兩條以上的select 語句組成,且每個查詢必須包含相同的列


???????? 全文本搜索

??? create table produce(note_id int auto_increment,prod_id char(10),note_date datetime,note_text text,primary key(note_id),fulltext(note_text));
??? select * from produce where match(note_text) against('linux');搜索note_text中包含linux的行,剛開始的時候因為賦值給prod_id時為了加'',導致沒搜索出來,后來改過來后就可以了,使用like來搜索也有差不多的結(jié)果,不過like返回的結(jié)果不是根據(jù)字符串偏前面的先返回


??? 布爾文本搜索

??? select note_text from produce where match(note_text) against('heavy -rope*' in boolean mode);只返回匹配heavy且不包含rope開頭的字符串,+代表必須存在


?? select note_text from produce where match(note_text) against('heavy ropes' in boolean mode);至少包含heavy或者ropes

?? select note_text from produce where match(note_text) against('"heavy ropes"' in boolean mode);包含短語heavy ropes

?? select note_text from produce where match(note_text) against('>heavy <rope' in boolean mode);優(yōu)先heavy 降低rope

?? select note_text from produce where match(note_text) against('+heavy +(<rope)' in boolean mode);兩個都必須存在,后者的優(yōu)先級降低


??

創(chuàng)建視圖

create view stuinfo as select * from student where id>2 and id<10;

select * from stuinfo;就能查看id在2到10之間的學生信息了,而且還可以把某種固定格式的搜索結(jié)果格式轉(zhuǎn)換為視圖,方便使用




??

?

轉(zhuǎn)載于:https://www.cnblogs.com/biaopei/p/7730668.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的mysql再探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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