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

歡迎訪問 生活随笔!

生活随笔

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

数据库

常用 SQL 语句汇总

發布時間:2023/12/20 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用 SQL 语句汇总 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

常用 SQL 匯總

使用索引注意事項

  • 使用like關鍵字時,前置%會導致索引失效。
  • 使用null值會被自動從索引中排除,索引一般不會建立在有空值的列上。
  • 使用or關鍵字時,or左右字段如果存在一個沒有索引,有索引字段也會失效。
  • 使用!=操作符時,將放棄使用索引。因為范圍不確定,使用索引效率不高,會被引擎自動改為全表掃描。
  • 不要在索引字段進行運算。
  • 在使用復合索引時,最左前綴原則,查詢時必須使用索引的第一個字段,否則索引失效;并且應盡量讓字段順序與索引順序一致。
  • 避免隱式轉換,定義的數據類型與傳入的數據類型保持一致。
  • 使用多列字段排序的時候,應當盡量使用同樣的排序規則,否則會導致索引失效。
  • 使用 lower 函數進行不區分大小寫比較時,應當先檢查字段的排序規則是否已經為 utf8_general_ci 不區分大小寫排序,如果已經是,則避免使用 lower 轉小寫進行比較,因為 lower 會導致索引失效。

【事務】

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

alter table table_name engine=InnoDB;

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

【復雜統計】

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

【根據查詢結果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,'%');

【字段數據轉移】

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

【區分大小寫查詢】

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

【時間偏移】

時間字段 <= 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計算時間差】

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

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

【按月統計數據】

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

參考網址: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/%";

【推薦數據修復】

查詢:

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;

【指定數據互換】

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);

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。