SELECT
mui.id,
mui.merchant_id,
mui.member_id,
DATE_FORMAT(
mui.recently_consume_time,
'%Y%m%d%H%i%s'
) recently_consume_time,
IFNULL(mui.total_consume_num, ) total_consume_num,
IFNULL(mui.total_consume_amount, ) total_consume_amount,
WHEN u.nick_name IS NULL THEN
WHEN u.nick_name = '' THEN
u.nick_name
) AS 'nickname',
u.sex,
u.head_image_url,
u.province,
u.city,
u.country
merchant_member_info mui
LEFT JOIN member_info u ON mui.member_id = u.id
WHERE
AND mui.merchant_id = '商户编号'
ORDER BY
mui.recently_consume_time DESC / ASC
LIMIT ,
# 删除近期消费时间索引
ALTER TABLE merchant_member_info DROP INDEX index_merchant_user_last_time;
# 删除商户编号索引
ALTER TABLE merchant_member_info DROP INDEX index_merchant_user_merchant_ids;
# 建立商户编号和近期消费时间组合索引
ALTER TABLE merchant_member_info ADD INDEX idx_merchant_id_recently_time (`merchant_id`,`recently_consume_time`);
SELECT
mui.id,
mui.merchant_id,
mui.member_id,
DATE_FORMAT(
mui.recently_consume_time,
'%Y%m%d%H%i%s'
) recently_consume_time,
IFNULL(mui.total_consume_num, ) total_consume_num,
IFNULL(mui.total_consume_amount, ) total_consume_amount,
WHEN u.nick_name IS NULL THEN
WHEN u.nick_name = '' THEN
u.nick_name
) AS 'nickname',
u.sex,
u.head_image_url,
u.province,
u.city,
u.country
merchant_member_info mui
INNER JOIN (
SELECT
merchant_member_info
WHERE
merchant_id = '商户ID'
ORDER BY
recently_consume_time DESC
LIMIT ,
) AS tmp ON tmp.id = mui.id