閱讀349 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Mysql常用sql方法

91316dc0gb328c6116405%26690.jpg


1、mysql 導出文件:

   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、關聯查詢

   sql中多個left join,為了保證返回數量和主表一樣,要加個group by 主表id


3、if,ifnull,concat_ws等常見方法

   1)concat_ws('',country, province, city) region  三字段按照''之間的內容合拚;

    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遠程連接-centos7

   首先登錄: 

   mysql -u root -h localhost -p

   use mysql                #打開mysql數據庫


   2)將host設置為%表示任何ip都能連接mysql,當然您也可以將host指定為某個ip

     update user set host='%' where user='root' and host='localhost';

     flush privileges;        #刷新權限表,使配置生效

     然後我們就能遠程連接我們的mysql了。


   3)如果您想關閉遠程連接,恢複mysql的默認設置(隻能本地連接),您可以通過以下步驟操作:

     use mysql                #打開mysql數據庫

     update user set host='localhost' where user='root';        #將host設置為localhost表示隻能本地連接mysql

     flush privileges;        #刷新權限表,使配置生效


     update user set password=password('123456') where User='root';#修改密碼

     flush privileges ; #刷新權限表,使配置生效

     備注:您也可以添加一個用戶名為yuancheng,密碼為123456,權限為%(表示任意ip都能連接)的遠程連接用戶。命令參考如下:

     grant all on *.* to 'yuancheng'@'%' identified by '123456';

     flush privileges;

   4)mysql排序時如果該字段是varchar怎麼辦?


     2種辦法:

     1. order by 字段+0

     2. order by cast(字段 as int)

     

6、批量修改字段數據

     update  t_comment SET  avatar = replace(avatar, 'http', 'https');//替換

     update t_log set message=concat("https",message);//前麵追加

最後更新:2017-09-25 15:32:56

  上一篇:go  確定不收藏?十張機器學習和深度學習工程師必備速查表!
  下一篇:go  書單推薦 | 數據挖掘和統計科學自學十大必備讀物