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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

常用 SQL 语句汇总

發(fā)布時(shí)間:2023/12/20 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用 SQL 语句汇总 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

常用 SQL 匯總

使用索引注意事項(xiàng)

  • 使用like關(guān)鍵字時(shí),前置%會(huì)導(dǎo)致索引失效。
  • 使用null值會(huì)被自動(dòng)從索引中排除,索引一般不會(huì)建立在有空值的列上。
  • 使用or關(guān)鍵字時(shí),or左右字段如果存在一個(gè)沒(méi)有索引,有索引字段也會(huì)失效。
  • 使用!=操作符時(shí),將放棄使用索引。因?yàn)榉秶淮_定,使用索引效率不高,會(huì)被引擎自動(dòng)改為全表掃描。
  • 不要在索引字段進(jìn)行運(yùn)算。
  • 在使用復(fù)合索引時(shí),最左前綴原則,查詢時(shí)必須使用索引的第一個(gè)字段,否則索引失效;并且應(yīng)盡量讓字段順序與索引順序一致。
  • 避免隱式轉(zhuǎn)換,定義的數(shù)據(jù)類(lèi)型與傳入的數(shù)據(jù)類(lèi)型保持一致。
  • 使用多列字段排序的時(shí)候,應(yīng)當(dāng)盡量使用同樣的排序規(guī)則,否則會(huì)導(dǎo)致索引失效。
  • 使用 lower 函數(shù)進(jìn)行不區(qū)分大小寫(xiě)比較時(shí),應(yīng)當(dāng)先檢查字段的排序規(guī)則是否已經(jīng)為 utf8_general_ci 不區(qū)分大小寫(xiě)排序,如果已經(jīng)是,則避免使用 lower 轉(zhuǎn)小寫(xiě)進(jìn)行比較,因?yàn)?lower 會(huì)導(dǎo)致索引失效。

【事務(wù)】

mysql的MyISAM引擎是不支持事務(wù)的,需要更改為Innodb

alter table table_name engine=InnoDB;

參考:http://www.linuxidc.com/Linux/2012-10/72884.htm

【復(fù)雜統(tǒng)計(jì)】

insert into web_statis(user,create_time,stone,dirt,orecoal,ironore,goldore,diamond,allore) select al.*,(stone+dirt+orecoal+ironore+goldore+diamond) as allore from (select cu.user, '2015-2-9 10:09:18' as create_time,(case when c1 is null then 0 else c1 end) as stone,(case when c2 is null then 0 else c2 end) as dirt,(case when c3 is null then 0 else c3 end) as orecoal ,(case when c4 is null then 0 else c4 end) as ironore,(case when c5 is null then 0 else c5 end) as goldore,(case when c6 is null then 0 else c6 end) as diamond from co_user culeft join (select count(*) c1,user from co_block where type in(1,4) and time >=1423353600 and time < 1423440000 group by user) tc1 on cu.rowid = tc1.userleft join (select count(*) c2,user from co_block where type in(2,3) and time >=1423353600 and time < 1423440000 group by user) tc2 on cu.rowid = tc2.userleft join (select count(*) c3,user from co_block where type = 16 and time >=1423353600 and time < 1423440000 group by user) tc3 on cu.rowid = tc3.userleft join (select count(*) c4,user from co_block where type = 15 and time >=1423353600 and time < 1423440000 group by user) tc4 on cu.rowid = tc4.userleft join (select count(*) c5,user from co_block where type = 14 and time >=1423353600 and time < 1423440000 group by user) tc5 on cu.rowid = tc5.userleft join (select count(*) c6,user from co_block where type = 56 and time >=1423353600 and time < 1423440000 group by user) tc6 on cu.rowid = tc6.user) al

【根據(jù)查詢結(jié)果update】

update A表 a INNER JOIN(select 字段1,字段2 from B表 b)t on t.字段2 = a.字段2 set a.字段3 = "XXX";

格式:update table_a inner join table_b on xxx = xxx set table_a.xxx = "xxx"

【反向like查詢】

SELECT id,keywords FROM wx_keywords WHERE '百度一下' LIKE CONCAT('%',keywords,'%');

【字段數(shù)據(jù)轉(zhuǎn)移】

update Devices as A left join Devices as B on A.ParentCode = B.Code set A.ParentID = B.ID;

【區(qū)分大小寫(xiě)查詢】

select * from wx_news where url like binary("%appid%");

【時(shí)間偏移】

時(shí)間字段 <= date_add(now(), interval - 6 MONTH)

參考:http://www.w3school.com.cn/sql/func_date_add.asp

MySQL 添加索引

1.添加PRIMARY KEY(主鍵索引) www.2cto.com mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )2.添加UNIQUE(唯一索引)mysql>ALTER TABLE `table_name` ADD UNIQUE (`column` )3.添加INDEX(普通索引) mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )4.添加FULLTEXT(全文索引) mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)5.添加多列索引 mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

【從小到大排序,0在最后】

select * from test order by seq=0,seq;

【MySQL計(jì)算時(shí)間差】

SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01');

interval可是:SECOND 秒、MINUTE 分鐘、HOUR、DAY 天、MONTH 月、YEAR 年

【按月統(tǒng)計(jì)數(shù)據(jù)】

select DATE_FORMAT(create_date,'%Y年%m月') months,sum(amount) amount from p2p_investment where transfer is null group by months;

參考網(wǎng)址:http://linkyou.blog.51cto.com/1332494/751980/

【替換URL域名】

update p2p_ad set url = concat("https://www.51checai.com/", substr(url,LENGTH("http://test.51checai.com/")+1)) where url like "http://test.51checai.com/%"; update `p2p_borrowing_material` set large = concat("https://img-51checai.b0.upaiyun.com/", substr(large,LENGTH("https://img.51checai.com/")+1)) where large like "https://img.51checai.com/%";

【推薦數(shù)據(jù)修復(fù)】

查詢:

select id,create_date,`referral_setting`,date_add(create_date, interval +180 day), CONCAT("{\"referralAmount\":10000,\"referralGift\":\"31\",\"referralGiftTwo\":\"32,33\",\"paymentTime\":\"2016-04-01\",\"finishTime\":\"",DATE_FORMAT(date_add(create_date, interval +180 day),'%Y-%m-%d'),"\",\"feeRate\":0.01}") from p2p_member where create_date >= "2016-04-01" and create_date < "2016-09-22" and `referrer` is not null order by id desc;

更新:

update p2p_member set `referral_setting` = CONCAT("{\"referralAmount\":10000,\"referralGift\":\"31,38\",\"referralGiftTwo\":\"32,33,39\",\"paymentTime\":\"2016-04-01\",\"finishTime\":\"",DATE_FORMAT(date_add(create_date, interval +180 day),'%Y-%m-%d'),"\",\"feeRate\":0.01}") where create_date >= "2016-09-22" and `referrer` is not null;

【指定數(shù)據(jù)互換】

update test t1 left join test t2 on t1.id = ((select 1+2)-t2.id ) set t1.position = t2.position where t1.id in (1,2);

總結(jié)

以上是生活随笔為你收集整理的常用 SQL 语句汇总的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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