zoukankan      html  css  js  c++  java
  • mysql 常用语句

    IF表达式

    IF(expr1,expr2,expr3)

    如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3

    select if(1=1,'aa','bb'), if(1=2,'aa','bb')

    1.直接执行语句

    mysql> show databases;

    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | metastore |
    | mysql |
    | test |
    +--------------------+
    4 rows in set (0.00 sec)

    2.执行sql脚本

    [root@host mysql]# cat sqltext.txt
    show databases;

    mysql> source /root/mysql/sqltext.txt
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | metastore |
    | mysql |
    | test |
    +--------------------+
    4 rows in set (0.00 sec)

    3.导出数据到文件

    windows下:

    [SQL]select * from role into OUTFILE 'd:/role.txt'

    受影响的行: 7
    时间: 0.004s

    linux下:

    mysql> select curdate() into outfile 'tianyongtao';
    Query OK, 1 row affected (0.00 sec)

    [root@host mysql]# pwd
    /var/lib/mysql
    [root@host mysql]# cat tianyongtao
    2018-02-07

    4.

    [root@host ~]# mysql -uroot -proot test -e "select * from role;" -N >aaaa
    [root@host ~]# cd /var/lib/mysql
    [root@host mysql]# pwd
    /var/lib/mysql
    [root@host mysql]# ls
    aaaa hive-schema-2.1.0.mysql.sql ibdata1 ib_logfile0 ib_logfile1 metastore mysql mysql.sock test tianyongtao
    [root@host mysql]# cat aaaa
    1 NULL 2017-11-16 14:49:11 henan luohe linying 1 10
    40 NULL 2017-11-13 14:50:25 guangdong shenzhen 1 20
    110 NULL 2017-11-14 14:50:47 beijing 1 20
    200 NULL 2017-11-14 14:49:47 shandong qingdao 0 8
    400 NULL 2017-11-15 14:49:56 anhui hefei 0 4
    600 NULL 2017-11-15 14:50:05 hunan changsha 0 91
    650 NULL 2017-11-01 17:24:34 NULL 1 29

    [root@host ~]# mysql -uroot -proot test -e "select * from role;" -N >/root/mysql/aaaa
    [root@host ~]# cd /root/mysql
    [root@host mysql]# ls
    aaaa sqltext.txt
    [root@host mysql]# cat aaaa
    1 NULL 2017-11-16 14:49:11 henan luohe linying 1 10
    40 NULL 2017-11-13 14:50:25 guangdong shenzhen 1 20
    110 NULL 2017-11-14 14:50:47 beijing 1 20
    200 NULL 2017-11-14 14:49:47 shandong qingdao 0 8
    400 NULL 2017-11-15 14:49:56 anhui hefei 0 4
    600 NULL 2017-11-15 14:50:05 hunan changsha 0 91
    650 NULL 2017-11-01 17:24:34 NULL 1 29

    [root@host mysql]# mysql -uroot -proot test </root/mysql/sqltext.txt
    Database
    information_schema
    metastore
    mysql
    test
    [root@host mysql]# mysql -uroot -proot test </root/mysql/sqltext.txt > /root/mysql/database
    [root@host mysql]# ls
    aaaa database sqltext.txt
    [root@host mysql]# pwd
    /root/mysql
    [root@host mysql]# cat database
    Database
    information_schema
    metastore
    mysql
    test

    查看权限

    show grants for root@'localhost';

    Linux下查看mysql、apache是否安装,并卸载。

    [root@host ~]# ps -ef|grep mysql
    root 16502 1 0 Jan19 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
    mysql 16607 16502 0 Jan19 pts/0 00:06:40 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
    root 17806 13982 0 Jan22 pts/0 00:00:00 mysql -u root -p
    root 21218 21062 0 15:46 pts/1 00:00:00 mysql -u root -p
    root 21437 21253 0 16:03 pts/2 00:00:00 grep mysql

    /var/lib/mysql: 数据库目录

    usr/lib/mysql:mysql的安装路径

  • 相关阅读:
    原码、反码、补码之间的相互关系
    在用 JavaScript 工作时,我们经常和条件语句打交道,这里有5条让你写出更好/干净的条件语句的建议。
    冒泡排序最佳情况的时间复杂度
    path的join和resolve的使用区别
    SCSS入门
    webpack 前后端分离开发接口调试解决方案,proxyTable解决方案
    JS中原型链的理解
    30分钟,让你彻底明白Promise原理
    状态码常用对照表
    前端性能优化方案都有哪些?
  • 原文地址:https://www.cnblogs.com/playforever/p/8426986.html
Copyright © 2011-2022 走看看