简体   繁体   English

如何通过MySQL中加密的字段查询结果顺序

[英]How to query result order by a field encrypted in mysql

There is a table with 100K rows data as below: 有一个包含10万行数据的表,如下所示:

CREATE TABLE `person_department_ref` (
  `id` bigint(12) NOT NULL AUTO_INCREMENT,
  `account` varchar(64) NOT NULL,
  `department_id` varchar(64) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  KEY `account_department_normal` (`account`,`department_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=282 DEFAULT CHARSET=utf8

I use Java code to encode "account" with AESEncrypt , and insert data to the table. 我使用Java代码使用AESEncrypt编码"account" ,并将数据插入表中。

I can use 我可以用

"select distinct r.account from person_department_ref r order by r.account"

with KEY(account_department_normal) if there is no encryption. 如果没有加密,请使用KEY(account_department_normal)。

So how can I query the same result with with encrypted data and using KEY? 那么,如何使用加密数据和KEY查询相同的结果?

If you will use order by to encrypted column then Mysql will give order by result on the basis of encrypted column so the data will not be match with the data of query which you mentioned. 如果您将对加密列使用按顺序排序,则Mysql将根据加密列对结果进行排序,因此数据将与您提到的查询数据不匹配。 In this, you can keep both the columns in your table (account and account_encypted_value) and you can retrieve the encrypted account data on the basis of normal account order by query. 在这种情况下,您可以将表中的两个列(account和account_encypted_value)都保留在表中,并且可以根据查询的正常帐户顺序检索加密的帐户数据。

select distinct r.account_encypted_value from person_department_ref r order by r.account

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM