mysql 常用语句(持续更新)

1 登录mysql
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.

2 显示mysql中有哪些数据库
mysql> show databases;

3 创建数据库
mysql> create database trace;
Query OK, 1 row affected (0.00 sec)

4 使用数据库trace
mysql> use trace;
Database changed

5 查询数据库trace中有哪些表
mysql> show tables;
mysql 常用语句(持续更新)

6 删除表emp
mysql> drop table emp;
Query OK, 0 rows affected (0.01 sec)

7 删除数据库trace
mysql> drop database trace;
Query OK, 72 rows affected (0.37 sec)

6 导入文件trace.sql   (我把trace.sql脚本文件放在了~下)
mysql> source ~/trace.sql;  

6 查看当前使用的哪个数据库

mysql> select database();
mysql 常用语句(持续更新)

 



主从篇

8 查看mysql主机状态和从机状态

查看mysql主机状态---->在主机上执行下述命令:
mysql> show master status;
mysql 常用语句(持续更新)

查看mysql从机状态---->在从机上执行下述命令:
mysql> show slave status\G;
mysql 常用语句(持续更新)

 

 



函数篇
myql已经给我们定义好了一些常用的函数,这些常用函数举例如下:

日期和时间常用函数:
1 curdate()  返回当前日期
mysql 常用语句(持续更新)
2 curtime()  返回当前时间
mysql 常用语句(持续更新)
3 now()  返回当前的日期和时间
mysql 常用语句(持续更新)
4 year(date)  返回日期的年份
---- 日期可以是以 xxxx-xx-xx 类型也可以是 xxxx/xx/xx类型
mysql 常用语句(持续更新)



自定义函数篇

1 查看自定义函数功能是否开启
mysql> show variables like '%func%';

如果开启,则显示:
mysql 常用语句(持续更新)
如果未开启,则显示:
mysql 常用语句(持续更新)

自定义函数功能开启命令:
mysql> set global log_bin_trust_function_creators=1;
Query OK, 0 rows affected (0.00 sec)

自定义函数功能关闭命令:
mysql> set global log_bin_trust_function_creators=0;
Query OK, 0 rows affected (0.00 sec)

注意:如果要使用自定义函数功能,则必须开启该功能才可以。

 



查询语句:

mysql> select m_type,m_status,create_time,busi_id from th_logs_message where m_status != 000 and create_time > '2018-11-20';
+--------+----------+---------------------+---------+
| m_type | m_status | create_time         | busi_id |
+--------+----------+---------------------+---------+
| 613    | 3276     | 2018-11-20 10:49:34 | 470     |
| 516    | 3277     | 2018-11-20 10:49:34 | 470     |
| 613    | 3279     | 2018-11-20 10:58:02 | 468     |
| 511    | 3334     | 2018-11-22 10:22:14 | 557     |
| 511    | 3349     | 2018-11-22 10:22:31 | 558     |
| 512    | 3351     | 2018-11-22 13:46:15 | 557     |
| 511    | 3353     | 2018-11-22 13:55:19 | 560     |
| 511    | 3354     | 2018-11-22 14:03:05 | 561     |
| 711    | 3385     | 2018-11-23 13:25:19 | 472     |
| 613    | 3386     | 2018-11-23 13:25:19 | 472     |
| 516    | 3387     | 2018-11-23 13:25:19 | 472     |
| 516    | 3453     | 2018-11-26 10:51:48 | 455     |
| 711    | 3454     | 2018-11-26 10:51:54 | 454     |
| 613    | 3455     | 2018-11-26 10:51:54 | 454     |
| 516    | 3456     | 2018-11-26 10:51:54 | 454     |
+--------+----------+---------------------+---------+
50 rows in set (0.00 sec)
mysql> select m_type,m_status,create_time,busi_id from th_logs_message where m_status = 000 and create_time > '2018-11-20'
    -> ;
+--------+----------+---------------------+-------------+
| m_type | m_status | create_time         | busi_id     |
+--------+----------+---------------------+-------------+
| 515    | 000      | 2018-11-20 09:46:54 | 468         |
| 515    | 000      | 2018-11-20 09:46:56 | 467         |
| 513    | 000      | 2018-11-20 09:55:09 | 545         |
| 611    | 000      | 2018-11-20 10:11:57 | 647         |
| 517    | 000      | 2018-11-20 11:15:46 | 54300000038 |
| 611    | 000      | 2018-11-21 09:44:15 | 656         |
| 511    | 000      | 2018-11-21 09:46:22 | 546         |
| 611    | 000      | 2018-11-23 11:59:40 | 665         |
| 517    | 000      | 2018-11-23 13:19:35 | 56300000029 |
| 611    | 000      | 2018-11-26 11:08:36 | 673         |
| 515    | 000      | 2018-11-26 11:09:39 | 477         |
| 511    | 000      | 2018-11-26 11:10:52 | 566         |
+--------+----------+---------------------+-------------+
154 rows in set (0.01 sec)

mysql> select * from th_logs_message where m_type=516 order by create_time desc limit 0,1;
+------+---------+----------------------------------------------------------------------------------------------------------------------------------+---------+----------+--------+------+---------------------+
| id   | busi_id | m_content                                                                                                                        | from_id | m_status | m_type | ver  | create_time         |
+------+---------+----------------------------------------------------------------------------------------------------------------------------------+---------+----------+--------+------+---------------------+
| 3473 | 477     | {"data":{"userId":517,"bindId":477},"busiId":"477","v":"1","mid":"5d9864a1-0790-4880-9da5-399e03b25c4b","from":"1","type":"516"} | 1       | 3473     | 516    | 1    | 2018-11-26 11:09:53 |
+------+---------+----------------------------------------------------------------------------------------------------------------------------------+---------+----------+--------+------+---------------------+
1 row in set (0.00 sec)