1

This is a cross-posting from an original question at How to check version is greater than some base value?

When calling MySQL SELECT @@VERSION; or SELECT VERSION();, I get for instance '5.7.11-log'. Is there a "well-known" way to check if the version is greater (or smaller) than some major.minor.patch version? If not a well-known way, is there a way without using a temporary table or a user-defined function (there probably is, but for a reason or another currently eludes me)? I would use this to check if there's support for JSON type type that was introduced in version 5.7.8.

Upon researching this more, it appears this is a bit tougher nut for my skills to crack. For instance, I could write something like

SELECT
    SUBSTRING_INDEX(@@VERSION, '.', 1) AS major, 
    SUBSTRING_INDEX(SUBSTRING_INDEX(@@VERSION,'.', 2), '.', -1) AS minor, 
    SUBSTRING_INDEX(SUBSTRING_INDEX(@@VERSION,'.', -2), '.', -1) AS patch;

but that isn't entirely satisfactory (e.g. see -log) and it doesn't directly check the existence of the feature.

Is anyone aware of better ways?

<edit: To amend Rick's answer a bit, the comments syntax can be used to recognize constructs too, not just versions. For instance, in this particular case like this SELECT /*!JSON_TYPE*/('["a", "b", 1]');. Here's a link to the documentation: http://dev.mysql.com/doc/refman/5.7/en/comments.html.

1

/*!50700 ... */ says: If you are running a version older than 5.7.0, this is a comment. Else it is sql. It can be entire statements or fragments of statements.

Example (taken from mysqldump output):

/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;

Presumably, @@TIME_ZONE was implemented in 4.1.3.

(Note: I think the two spaces are part of the syntax.)

  • This is exactly what the doctor has ordered! Cheers! – Veksi Jan 17 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.