SELECT
mhy_order.id AS id,
mhy_order.id AS orderId,
mhy_trademark.id AS trademarkId,
mhy_trademark_type.id AS typeId,
mhy_order.order_type AS type,
mhy_trademark_type.type AS category,
mhy_trademark.`name` AS trademark,
mhy_trademark_type.registration_number AS registrationNumber,
mhy_applicant.`name` AS applicantName,
mhy_applicant.message_corp AS messageCorp,
IFNULL( mhy_order.complete, '否' ) AS isComplete,
mhy_applicant.custom_name AS contactUser,
mhy_order.charge AS charge,
mhy_order.notification_method AS noticeType,
mhy_order.notification_time AS noticeTime,
sys_user.username AS salesman,
mhy_order_progress.gen_time AS messageTime,
ROW_NUMBER() over ( PARTITION BY mhy_order_progress.order_id ORDER BY mhy_order_progress.gen_time DESC ) RowNum,
FROM
`mhy_trademark_type`
JOIN mhy_trademark ON `mhy_trademark_type`.trademark_id = mhy_trademark.id
JOIN mhy_order ON mhy_trademark.order_id = mhy_order.id
JOIN mhy_applicant ON mhy_order.applicant_id = mhy_applicant.id
JOIN `sys_user` ON `sys_user`.user_id = mhy_order.create_user_id -- JOIN mhy_applicant as tp ON mhy_order.transferor_id = tp.id
LEFT JOIN mhy_order_progress ON mhy_order_progress.order_id = mhy_order.id
目前的 SQL 就是这样 还是比较简单的,关键在于待分组的表 mhy_order_progress 是 left join 的,可能关联不到这张表。 导致 ```PARTITION BY mhy_order_progress.order_id`` 执行时,数据都跑一个分区里了,因为 mhy_order_progress.order_id 都是 null 。
数据库是 mysql8.0 最新版本。这个 SQL 还有个非常长的 where 以及分页。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.