Mysql常用sql语句汇总

作者:水灵儿 时间:2024-01-21 04:31:48 

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);//前面追加

来源:https://yq.aliyun.com/articles/215966

标签:Mysql,常用语句
0
投稿

猜你喜欢

  • 基于python制作简易版学生信息管理系统

    2022-04-15 18:39:45
  • Python for Informatics 第11章 正则表达式(一)

    2021-01-27 06:43:43
  • 有用的SQL语句(删除重复记录,收缩日志)

    2008-03-04 16:59:00
  • XML简易教程之一

    2008-09-05 17:19:00
  • Python和C语言利用栈分别实现进制转换

    2022-10-02 15:43:15
  • MySQL两种表存储结构MyISAM和InnoDB的性能比较测试

    2024-01-28 02:35:55
  • Python与C++中梯度方向直方图的实现

    2022-03-17 20:37:34
  • 详解java连接mysql数据库的五种方式

    2024-01-19 06:38:21
  • Mysql经典高逼格/命令行操作(速成)(推荐)

    2024-01-26 14:45:01
  • Mysql合并结果接横向拼接字段的实现步骤

    2024-01-19 13:39:35
  • swfupload上传使用代码说明ASP版

    2011-11-27 09:34:32
  • SQL查询效率注意事项小结

    2012-01-05 19:31:25
  • Python实现PING命令的示例代码

    2022-01-10 09:19:15
  • Python序列化模块JSON与Pickle

    2022-11-06 00:24:37
  • Python下载手机小视频的操作方法

    2022-11-16 02:40:55
  • keras获得model中某一层的某一个Tensor的输出维度教程

    2023-12-15 11:28:54
  • python实现画出e指数函数的图像

    2023-09-24 22:12:10
  • 使用pyinstaller逆向.pyc文件

    2021-09-10 01:34:23
  • 如何防范利用刷新来“作弊”的计数器?

    2009-11-22 17:18:00
  • 解决Pytorch训练过程中loss不下降的问题

    2023-03-01 09:30:22
  • asp之家 网络编程 m.aspxhome.com