2016-06-21 1 views
2

У меня есть набор данных в базе данных MySQL. Я получаю список результатов, упорядоченных по полю под названием login. Когда я получаю этот набор, две строки находятся в следующем порядке.MySQL ORDER BY сравнение vs Java сравнение сравнения

[email protected] 
[email protected] 

Однако, когда я сравнить их в Java (необходимо из-за последующей обработки, необходимой для слияния приложения на стороне объектов), [email protected] имеет значение меньше, чем [email protected] Другими словами, сравнение String ожидало бы, что они будут в следующем порядке.

[email protected] 
[email protected] 

В целом все остальное возвращается в правильном порядке. Я предполагаю, что разница здесь, скорее всего, является разницей в том, как сравнение строк Java и MySQL обрабатывает определенные значения. Как я могу заставить их вернуться в последовательном порядке (я в порядке с ними в любом случае, просто нужно согласованность).

Query Я бегу:

select t0.id as envUserId , t0.environment_id as envId, t0.environment_name as envName, t0.customer_name as customerName, t0.version version, t0.user_id as userId, t0.login as userLogin, t0.sso_granted_roles as sso_granted_roles, t1z_.role_name as defaultRole, t3.id as customRoleId, t3.name as customRoleName 
    from environment_user t0 
    left join ( 
    select distinct eu.id, eu.login  
    from environment_user eu     
    left join environment_user_role eur on eu.id = eur.environment_user_id 
    left join environment_user_custom_role eucr on eu.id = eucr.environment_user_id 
    left join custom_role cr on eucr.custom_role_id = cr.id   
    where eu.environment_id = '5a83069a-70d2-4d0e-9847-c709725281c5'    
    and (eur.role_name in ('Role1','Role2') 
     or cr.name in ('Role1','Role2'))  
    order by eu.login limit 0, 200) f on t0.id = f.id 
    left outer join environment_user_role t1z_ on t1z_.environment_user_id = t0.id 
    left outer join environment_user_custom_role ct1z_ on ct1z_.environment_user_id = t0.id 
    left outer join custom_role t3 on t3.id = ct1z_.custom_role_id 
    where t0.environment_id = '5a83069a-70d2-4d0e-9847-c709725281c5'  
    and t0.id = f.id 
    order by userLogin asc 

Что я получаю обратно (дополнительные строки выше и ниже, были удалены для ясности)

'c2ad9f82-e0d5-4f8d-a5fe-a2d72d901b98', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '649ea0bc-dab7-4ad2-a534-546f9817e252', '[email protected]', '0', 'Role1', NULL, NULL 
'83313002-49a3-45f2-9013-e8dab15789d5', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '40d5c22a-33f8-4a37-a4db-63e3709cfae7', '[email protected]', '0', 'Role1', NULL, NULL 
'5ba69c88-a773-4d5b-835d-c88688867d6a', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '91a7609a-4809-4e27-9d6f-448ff62b38b3', '[email protected]', '0', 'Role1', NULL, NULL 
'6833a699-b5ca-46aa-8a53-23a6ef41e1f8', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '718808fa-3799-457f-9cdb-88ef887e0492', '[email protected]', '0', 'Role1', NULL, NULL 
'c466c478-8a32-4926-9cde-06a40071ac85', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '6282739d-76ea-4dbb-be5e-b7d64d3b3f3f', '[email protected]', '0', 'Role1', NULL, NULL 
'5b04d561-6c20-4703-aa96-f17eda0405b6', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', 'fb644427-46ab-42e4-8295-65a397409c0d', '[email protected]', '0', 'Role1', NULL, NULL 
'27116bed-a1a6-483c-9e7b-97158786245c', '5a83069a-70d2-4d0e-9847-c709725281c5', 'SearchTestDomainEnv', 'SearchTestDomainCustomer', '1', '246f392d-6d27-402e-837f-98384da0abb6', '[email protected]', '0', 'Role1', NULL, NULL 
+2

Прочитать информацию о том, как они хранят/сортируют строки и записывают в Java 'Компаратор ', который реализует эту же логику. – SJuan76

+0

Можете ли вы предоставить SQL-скрипт, который представляет ваш опыт? Поскольку я не могу воспроизвести упорядоченное SQL-заказы (если вы не заказываете в порядке убывания): http://sqlfiddle.com/#!9/9eecb7d/65180 – nasukkin

+0

Невозможно предоставить SQLFiddle без копирования в большинстве нашей схеме (которой мне не разрешено делать). Я добавил дополнительную информацию, надеюсь, это поможет. – kfan

ответ

0

Найдено 0 вопросов по this Вопросы переполнения стека. Похоже, что лексикографическая сортировка Java не основана на естественном языке, как реализована в utf8_unicode_ci. Решением здесь является создание Collator и использование метода compareTo для Collator для выполнения нашего сортировки.

3

То, что я узнал, что:

1) Когда я делаю запрос в MySQL, я получаю результат следующим образом:

enter image description here

2) После Test.java попытки найти порядок двух строк следующим образом:

import java.util.Arrays; 

public class Test { 
    public static void main(String[] args) { 
     String[] arr={"[email protected]", "[email protected]"}; 
     Arrays.sort(arr); 
     System.out.println(Arrays.toString(arr)); 
    } 
} 

и выход:

enter image description here

3) Чтобы увидеть параметры сортировки, после запроса Выполняется:

SELECT table_catalog, 
     table_schema, 
     table_name, 
     column_name, 
     collation_name 
FROM information_schema.columns 
WHERE table_schema = 'test' 
     AND column_name = 'email'; 

A й выход:

enter image description here

Итак, мы видим, что как MySQL и Java сортировать две строки в том же порядке, когда MySQL сортировки utf8_general_ci.

+0

Я добавил дополнительную информацию к сообщению выше. Не могли бы вы снова взглянуть? Я все еще могу воспроизвести эту проблему. Запуск MySql 5.6.23 MySQL Community Server (GPL), если это имеет значение. – kfan

+0

Это зависит от того, что использует ваша база данных. Какой из них вы используете? – Andreas

+0

@ Andreas Я отредактировал свой ответ. Не могли бы вы взглянуть на него? –

0

Смотрите comment на String.compareTo()

/** 
    * Compares two strings lexicographically. 
    * The comparison is based on the Unicode value of each character in 
    * the strings. The character sequence represented by this 
    * {@code String} object is compared lexicographically to the 
    * character sequence represented by the argument string. The result is 
    * a negative integer if this {@code String} object 
    * lexicographically precedes the argument string. The result is a 
    * positive integer if this {@code String} object lexicographically 
    * follows the argument string. The result is zero if the strings 
    * are equal; {@code compareTo} returns {@code 0} exactly when 
    * the {@link #equals(Object)} method would return {@code true}. 
    * <p> 
    * This is the definition of lexicographic ordering. If two strings are 
    * different, then either they have different characters at some index 
    * that is a valid index for both strings, or their lengths are different, 
    * or both. If they have different characters at one or more index 
    * positions, let <i>k</i> be the smallest such index; then the string 
    * whose character at position <i>k</i> has the smaller value, as 
    * determined by using the &lt; operator, lexicographically precedes the 
    * other string. In this case, {@code compareTo} returns the 
    * difference of the two character values at position {@code k} in 
    * the two string -- that is, the value: 
    * <blockquote><pre> 
    * this.charAt(k)-anotherString.charAt(k) 
    * </pre></blockquote> 
    * If there is no index position at which they differ, then the shorter 
    * string lexicographically precedes the longer string. In this case, 
    * {@code compareTo} returns the difference of the lengths of the 
    * strings -- that is, the value: 
    * <blockquote><pre> 
    * this.length()-anotherString.length() 
    * </pre></blockquote> 
    * 
    * @param anotherString the {@code String} to be compared. 
    * @return the value {@code 0} if the argument string is equal to 
    *   this string; a value less than {@code 0} if this string 
    *   is lexicographically less than the string argument; and a 
    *   value greater than {@code 0} if this string is 
    *   lexicographically greater than the string argument. 
    */ 
    public int compareTo(String anotherString) { 
     int len1 = value.length; 
     int len2 = anotherString.value.length; 
     int lim = Math.min(len1, len2); 
     char v1[] = value; 
     char v2[] = anotherString.value; 

     int k = 0; 
     while (k < lim) { 
      char c1 = v1[k]; 
      char c2 = v2[k]; 
      if (c1 != c2) { 
       return c1 - c2; 
      } 
      k++; 
     } 
     return len1 - len2; 
    } 

MySql сделать аналогичным образом, но зависит от character set БД. Дополнительная информация здесь: MYSQL

+0

Я понимаю, что такое лексикографическое упорядочение и как Java и MySQL используют его для сравнения строк, я пытаюсь понять, почему я получаю здесь разницу в сравнении. Судя по тому, что говорили выше люди, скорее всего, это связано с сортировкой. – kfan

Смежные вопросы