MySQL中字母数字字符串(java uuid)的比较(>,<)

时间:2019-01-24 08:56:19

标签: mysql

我有一张桌子说文件,如下图所示。

+--------+----------------------------------+
| userid | documentid                       |
+--------+----------------------------------+
| 949489 | 0001420ff9bc40298fcdf6a728be6541 |
| 949489 | 0001ed99f8be48748936bb0d1ddb8fa1 |
| 949489 | 0001faeb412d4f3aae340e3a435715cc |
| 949489 | 00022428dce34902b3cbc4ac1b832963 |
| 949489 | 0003488d119d4456a728d0804ea46f90 |
| 949489 | 00046632106d4baa9bb2fbd9fa87a137 |
| 949489 | 0004de1b7efb4845979221dce38c0b61 |
| 949489 | 00058877ed4a43328b8c364cdca187e1 |
| 949489 | 00059e6cab3349d48861df5a0c679d27 |

在此,documentid(java uuid)列是默认情况下索引的主键。我需要对documentid执行“ > ”操作。

有人可以详细解释一下mysql如何执行字母数字字符串比较吗?还会有任何性能问题吗?

1 个答案:

答案 0 :(得分:0)

下一个问题,请直接提供这样的脚本数据,这并不难,对我们来说将更简单。

CREATE TABLE `test_so`.`Files` (
     `userId` INT NOT NULL ,
     `documentId` VARCHAR(32) NOT NULL ,
      PRIMARY KEY (`documentId`)
 ) ENGINE = InnoDB;



INSERT INTO `Files`(`userId`, `documentId`) VALUES
(949489,'0001420ff9bc40298fcdf6a728be6541'),
(949489,'0001ed99f8be48748936bb0d1ddb8fa1'),
(949489,'0001faeb412d4f3aae340e3a435715cc'),
(949489,'00022428dce34902b3cbc4ac1b832963'),
(949489,'0003488d119d4456a728d0804ea46f90'),
(949489,'00046632106d4baa9bb2fbd9fa87a137'),
(949489,'0004de1b7efb4845979221dce38c0b61'),
(949489,'00058877ed4a43328b8c364cdca187e1'),
(949489,'00059e6cab3349d48861df5a0c679d27')

要了解如何执行某些查询,请执行以下操作:

SELECT '0000abcd00' < '0000abce00'   -- will display 1 means it's true

另一个要理解的查询:

SELECT * FROM `Files` ORDER BY `documentId` DESC

输出:

|userId |documentId                      |
|-------|--------------------------------|  
|949489 |00059e6cab3349d48861df5a0c679d27|  
|949489 |00058877ed4a43328b8c364cdca187e1|  
|949489 |0004de1b7efb4845979221dce38c0b61|  
|949489 |00046632106d4baa9bb2fbd9fa87a137|  
|949489 |0003488d119d4456a728d0804ea46f90|  
|949489 |00022428dce34902b3cbc4ac1b832963|  
|949489 |0001faeb412d4f3aae340e3a435715cc|  
|949489 |0001ed99f8be48748936bb0d1ddb8fa1|  
|949489 |0001420ff9bc40298fcdf6a728be6541|  

另一个查询,以获取最小和最大DocumentId:

SELECT `userId`, MAX(documentId), MIN(documentId) FROM `Files` GROUP BY `userId`

输出:

|userId |MAX(documentId)                      |MIN(documentId)                      |
|-------|-------------------------------------|-------------------------------------|
|949489 |00059e6cab3349d48861df5a0c679d27     |0001420ff9bc40298fcdf6a728be6541     |

最后,是的,您可以比较2个字符串,它只会看起来是字母数字顺序。