mysql再探
select子句及其順序
select from where group by having order by limit ??
創建表
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 數據存儲在內存不是磁盤,速度快,適合臨時表,myisam性能極高的引擎,支持全文本搜索,但不支持事物處理
插入數據
insert into student values(123,'linux',22);
insert into student values(123,'linux',22),(11,'test',23);同時插入兩條數據
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;插入檢索出的數據
更新數據
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
刪除數據
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且后面跟一個字符的學生,如果需要區分大小寫,只需要在regexp后面加關鍵字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 '\\-';列出包含字符-的學生,\\用來轉義
? ?匹配字符類
??? [:alnum:] 任意字符和數字[a-zA-Z0-9]
??? [:alpha:]? 任意字符[a-zA-Z]
??? [:blank:]? 空格和制表[\\t]
??? [:cntrl:]??? ASCII控制字符,ASCII 0-31和127
??? [:digit:]??? 任意數字[0-9]
??? [:graph:]? 與[:print:]相同,但不包括空格
??? [:lower:]?? 任意小寫字母[a-z]
??? [:upper:]?? 任意大寫字母[A-Z]
??? [:print:]???? 任意可打印字符
??? [:punct:]?? 既不在[:alnum:]又不在[:cntrl:]中的任意字符
??? [:xdigit:]??? 任意十六進制數字[a-fA-F0-o]
??? [:space:]?? 包括空格在內的任意空白字符[\\f\\n\\r\\t\\v]
??? 匹配多個實例
???
??? *???? ? ? ?? 0個或多個匹配
? ? +?????????? 1個或多個匹配
?? ??????????? 0個或1個匹配
??? {n}???????? 指定數目匹配
??? {n,}??????? 大于或等于n次的匹配
??? {n,m}???? 匹配數目為n到m
???
??? select? * from student where name regexp '\\([0-9] bp?\\)';名字包含(1 bpa)之類的
???select? * from student where name regexp '[[:digit:]]{4}';名字包含4個數字的學生
????定位符
??
????? ^?????????????????? 文本的開始
????? $?????????????????? 文本的結束
????? [[:<:]]???????????? 詞的開始
????? [[:>:]]???????????? 詞的結束
????? select? * from student where id regexp '^[0-9\\.]';等價'^[0-9|\\.]即數字開頭或.開頭,注意區分,如果^在[ ]里面,是否定該集合的意思,在[ ]外面就是開頭的意思
????
????? 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;增加一行統計價格并命名為money
??? select upper(name) as linux from student;將名字轉換為大寫,lower()小寫,left()返回串左邊的字符,right()返回串右邊的字符,locate()找出串的一個子串,soundex()返回串的soundex值,即發音差不多的,如lee和lie等等。
??? 時間和日期處理函數
?
??? adddate()???????????????????? 增加一個日期
??? addtime()???????????????????? 增加一個時間
??? curdate()????????????????????? 返回當前時間
??? curtime()????????????????????? 返回當前時間
??? date()?????????????????????????? 返回日期時間的日期部分
??? datediff()????????????????????? 計算兩個日期之差
??? day()? hour()?? minute()?? month()??? now()?? second()?? year()?? 返回對應時間
???
??? select * from orders where date(order_date)='2017-5-1' ;如果只知道查找某一天的,但是訂單里的日期有包括了具體時刻,那么將其轉換為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;年月
??? 數值處理函數
??? abs???????? 絕對值
??? cos???????? 余弦值
??? exp???????? 指數值
??? mod??????? 余數
??? pi??????????? 圓周率
??? rand??????? 隨機數
??? sin????????? 正弦值
??? sqrt???????? 平凡根
??? tan????????? 正切值
?? 匯總數據
???
??? avg???????? 某列的平均值
??? count????? 某列的行數
??? max??????? 某列的最大值
??? min???????? 某列的最小值
??? sum??????? 某列值之和
??? select min(id) from student;? 打印最小的id
??? select sum(price*counts) as money from produces;統計總價
???
??? 分組數據
???
??? select id,count(*) from student group by id;根據id分組,并且統計每組id數據數量
??? select id,count(*) from student group by id having count(*)>=2;打印id組數量大于2的,having相當跟where差不多,唯一的差別在于where過濾行,having過濾組
???
???select id,count(*) from student where id>=2 group by id having count(*)>=2;
??? group by 和 order by 的區別:
??? order by 產生有序的的輸出,group by 分組行,但輸出可能不是分組的順序
???
???? select id,sum(id*age) from student group by id order by sum(id*age); 計算每個id組里面id*age的總和,再根據和排序
??? 子查詢
???
???? 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
??
???? 笛卡兒積(搜索結果行的數目是地一個表的行數乘以第二個表的行數)
???? select vend_name,prod_name,prod_price? from vendors,products order by vend_name,prod_name;
???? 內部聯結
???? select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id;?? 內部聯結inner join on?? 聯結條件用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,其區別是左連接時,如果右邊沒有數據,那么只會顯示左邊的數據,右邊顯示null,右外連接則相反
?? 聯合查詢
?? 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來搜索也有差不多的結果,不過like返回的結果不是根據字符串偏前面的先返回
??? 布爾文本搜索
??? 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);優先heavy 降低rope
?? select note_text from produce where match(note_text) against('+heavy +(<rope)' in boolean mode);兩個都必須存在,后者的優先級降低
??
創建視圖
create view stuinfo as select * from student where id>2 and id<10;
select * from stuinfo;就能查看id在2到10之間的學生信息了,而且還可以把某種固定格式的搜索結果格式轉換為視圖,方便使用
??
?
轉載于:https://www.cnblogs.com/biaopei/p/7730668.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: 蚂蚁金服CTO程立:AI尚不具备金融级的
- 下一篇: ETL概念集锦