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的安装路径

原文地址:https://www.cnblogs.com/playforever/p/8426986.html