SELECT
*
FROM
xc_reporting_task_detail rtd,
( SELECT max( id ) id FROM xc_reporting_task_detail GROUP BY task_id ) rtd2
WHERE
rtd.id = rtd2.id;
SELECT
*
FROM
xc_reporting_task_detail
WHERE
id IN ( SELECT MAX( id ) FROM xc_reporting_task_detail GROUP BY task_id )
3.如果排序字段不是id 是update_date
SELECT
*
FROM
( SELECT rtd.task_id, MAX( rtd.update_date ) AS update_date FROM xc_reporting_task_detail rtd GROUP BY rtd.task_id ) rtd
INNER JOIN xc_reporting_task_detail f ON f.task_id = rtd.task_id
AND f.update_date = rtd.update_date
sql數(shù)據(jù)
DROP TABLE IF EXISTS xc_reporting_task_detail
;
CREATE TABLE xc_reporting_task_detail
(
id
int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
task_id
int(11) NOT NULL DEFAULT 0 COMMENT '辦件id',
task_code
varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '辦件編碼',
item_code
varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '事項編碼',
remarks
varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '審批意見',
status
char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '狀態(tài),0-待填報,1-待審核, 2-通過贬媒,3-駁回, 4-暫存',
update_date
datetime(0) NOT NULL,
update_by
varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
create_date
datetime(0) NOT NULL,
create_name
varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '創(chuàng)建人姓名',
create_by
varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
del_flag
char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '刪除標(biāo)志 0正常 1刪除',
PRIMARY KEY (id
) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '填報任務(wù)詳情表' ROW_FORMAT = Dynamic;
-- Records of xc_reporting_task_detail
INSERT INTO xc_reporting_task_detail
VALUES (7, 2807, '330000000000210113000006', '330000000000000001', '通過', '2', '2021-01-13 20:27:30', '1', '2021-01-13 20:27:30', '超級管理員', '1', '0');
INSERT INTO xc_reporting_task_detail
VALUES (8, 2808, 'xxx', '000000000000000001', '666666不讓你通過', '3', '2021-01-14 15:43:49', '1', '2021-01-14 15:43:49', '超級管理員', '1', '0');
INSERT INTO xc_reporting_task_detail
VALUES (9, 2808, 'xxx', '000000000000000001', '666666不讓你通過2', '3', '2021-01-14 15:43:49', '1', '2021-01-14 15:43:49', '超級管理員', '1', '0');
INSERT INTO xc_reporting_task_detail
VALUES (10, 2807, '330000000000210113000006', '330000000000000001', '駁回', '3', '2021-01-13 20:27:30', '1', '2021-01-13 20:27:30', '超級管理員', '1', '0');