MySql GROUP_CONCAT每次执行语句时数据顺序变化

sql语句:

SELECT 
  u.user_id AS userId,
  GROUP_CONCAT(uc.user_car_id) AS userCarId,
  u.mobile,
  ui.real_name AS realName,
  GROUP_CONCAT(uc.plate_no) AS plateNo 
FROM
  customer_user AS u 
  JOIN customer_user_info AS ui 
    ON u.user_id = ui.user_id 
  JOIN customer_user_car AS uc 
    ON u.user_id = uc.user_id 
WHERE uc.is_delete = 0 
  AND u.store_number = 10001 
  AND uc.store_number = 10001 
GROUP BY u.user_id 

执行第一次:
MySql GROUP_CONCAT每次执行语句时数据顺序变化_第1张图片
注意这两字段,顺序会发生变化。

执行第二次:
MySql GROUP_CONCAT每次执行语句时数据顺序变化_第2张图片

执行第三次:
MySql GROUP_CONCAT每次执行语句时数据顺序变化_第3张图片

可以看到,每次执行,数据顺序都会发生变化。

解决:

SELECT 
  u.user_id AS userId,
  GROUP_CONCAT(
    uc.user_car_id 
    ORDER BY uc.user_car_id
  ) AS userCarId,
  u.mobile,
  ui.real_name AS realName,
  GROUP_CONCAT(uc.plate_no 
    ORDER BY uc.plate_no) AS plateNo 
FROM
  customer_user AS u 
  JOIN customer_user_info AS ui 
    ON u.user_id = ui.user_id 
  JOIN customer_user_car AS uc 
    ON u.user_id = uc.user_id 
WHERE uc.is_delete = 0 
  AND u.store_number = 10001 
  AND uc.store_number = 10001 
GROUP BY u.user_id 

只需要在GROUP_CONCAT()里面加上ORDER BY 数据的顺序就不会发生变化了。

你可能感兴趣的:(java)