Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mariadb - How do I print a query by MySQL's version in sql file?

I want to print query by MySQL's version in .sql file.

When I use MySQL version 5.6, want to print (2 column)

SELECT user, host FROM mysql.user;

or, I use MySQL version 5.7 or 8, want to print (3 column)

SELECT user, host, account_locked FROM mysql.user;

Because MySQL 5.6 version doesn't have account_locked column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
mysql> SELECT * FROM information_schema.GLOBAL_VARIABLES WHERE variable_name = 'version';
+---------------+-----------------+
| VARIABLE_NAME | VARIABLE_VALUE  |
+---------------+-----------------+
| VERSION       | 5.6.22-71.0-log |
+---------------+-----------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'version';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| version       | 5.6.22-71.0-log |
+---------------+-----------------+
1 row in set (0.00 sec)

mysql> SELECT @@version;
+-----------------+
| @@version       |
+-----------------+
| 5.6.22-71.0-log |
+-----------------+
1 row in set (0.00 sec)

In a Stored Procedure, you can do something like

IF (@@version < "5.7") THEN
    SELECT user, host FROM mysql.user;
ELSE
    SELECT user, host, account_locked FROM mysql.user;
ENDIF;

It gets messier if you also need to handle MariaDB, with its "10.x.y". And, if Percona has any differences, you need to dig deeper into the "version".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...