Mysql常用sql語句匯總
1、mysql 導(dǎo)出文件:
SELECT `pe2e_user_to_company`.company_name, `pe2e_user_to_company`.company_code, `users`.name, `users`.uid, `users`.mail, `pe2e_email_notification_email`.`email_cc` FROM `users` , `pe2e_user_to_company` LEFT JOIN `pe2e_email_notification_email` ON `pe2e_user_to_company`.`uid` = `pe2e_email_notification_email`.`uid` WHERE `users`.`uid` = `pe2e_user_to_company`.`uid` into outfile '/tmp/users.csv' fields terminated by ',' enclosed by '"' lines terminated by '\r\n';
2、關(guān)聯(lián)查詢
sql中多個left join,為了保證返回?cái)?shù)量和主表一樣,要加個group by 主表id
3、if,ifnull,concat_ws等常見方法
1)concat_ws('',country, province, city) region 三字段按照''之間的內(nèi)容合拼;
concat_ws('',CASE p.gameType1 WHEN 1 THEN '朗誦講故事' WHEN 2 THEN '朗誦情景演講' END,CASE p.gameType2 WHEN 3 THEN '主題寫作' END) as gameType;
2)if(gender=1,'男','女') as gender;
3)ifnull(age,0) as age;
4)(CASE ageGroup WHEN 1 THEN '兒童A組' WHEN 2 THEN '兒童B組' WHEN 3 THEN '少年A組' WHEN 4 THEN '少年B組' END) as ageGroup;
4、mysql5.7 找回root密碼
[root@166087 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --datadir=/data/mysql --skip-grant-tables
mysql> update user set authentication_string=password('123456') where user='root';
5、阿里云使用筆記-MySQL遠(yuǎn)程連接-centos7
首先登錄:
mysql -u root -h localhost -p
use mysql #打開mysql數(shù)據(jù)庫
2)將host設(shè)置為%表示任何ip都能連接mysql,當(dāng)然您也可以將host指定為某個ip
update user set host='%' where user='root' and host='localhost'; flush privileges; #刷新權(quán)限表,使配置生效
然后我們就能遠(yuǎn)程連接我們的mysql了。
3)如果您想關(guān)閉遠(yuǎn)程連接,恢復(fù)mysql的默認(rèn)設(shè)置(只能本地連接),您可以通過以下步驟操作:
use mysql #打開mysql數(shù)據(jù)庫
update user set host='localhost' where user='root'; #將host設(shè)置為localhost表示只能本地連接mysql
flush privileges; #刷新權(quán)限表,使配置生效
update user set password=password('123456') where User='root';#修改密碼
flush privileges ; #刷新權(quán)限表,使配置生效
備注:您也可以添加一個用戶名為yuancheng,密碼為123456,權(quán)限為%(表示任意ip都能連接)的遠(yuǎn)程連接用戶。命令參考如下:
grant all on *.* to 'yuancheng'@'%' identified by '123456'; flush privileges;
4)mysql排序時(shí)如果該字段是varchar怎么辦?
2種辦法:
1. order by 字段+0
2. order by cast(字段 as int)
6、批量修改字段數(shù)據(jù)
update t_comment SET avatar = replace(avatar, 'http', 'https');//替換
update t_log set message=concat("https",message);//前面追加
相關(guān)文章
MySQL使用集合函數(shù)進(jìn)行查詢操作實(shí)例詳解
這篇文章主要介紹了MySQL使用集合函數(shù)進(jìn)行查詢操作,結(jié)合實(shí)例形式詳細(xì)分析了MySQL使用集合函數(shù)進(jìn)行的運(yùn)算與查詢操作使用技巧,需要的朋友可以參考下2018-06-06
Mysql最新版本的數(shù)據(jù)庫安裝教程(5.7)
這篇文章主要為大家詳細(xì)介紹了Mysql最新版本的數(shù)據(jù)庫安裝教程,分享了Mysql 5.7安裝配置方法,感興趣的小伙伴們可以參考一下2016-07-07
關(guān)于MySQL自增ID的一些小問題總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于MySQL自增ID的一些小問題,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
MySQL8.0.3 RC版即將發(fā)布 先來看看有哪些變化
MySQL8.0.3 RC版即將發(fā)布,這篇文章主要介紹了MySQL8.0.3 RC版的一些新變化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
MySQL自定義序列數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了MySQL自定義序列數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
linux下mysql提示"mysql deamon failed to start"錯誤的解決方法
網(wǎng)站突然連接不上數(shù)據(jù)庫,于是朋友直接重啟了一下服務(wù)器。進(jìn)到cli模式下,執(zhí)行 service myqsld start 發(fā)現(xiàn)還是提示"mysql deamon failed to start"錯誤信息2013-04-04

