查询每人每天最后一条记录

2019-12-10  本文已影响0人  a9b854aded01

CREATE TABLE io_record (
id int(11) NOT NULL,
user_s_id int(255) NOT NULL COMMENT '消费方id',
cost decimal(10,2) NOT NULL COMMENT '消费金额',
balance decimal(10,2) NOT NULL COMMENT '余额',
add_time datetime NOT NULL COMMENT '时间',
mode varchar(255) NOT NULL COMMENT '消费方式',
mobile_md5 varchar(255) DEFAULT NULL COMMENT '手机号md5',
product_id int(11) DEFAULT NULL COMMENT '输出内容id',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- Records of io_record


BEGIN;
INSERT INTO io_record VALUES (1, 1, 1.00, 10.00, '2019-12-10 11:51:43', '1', '123123123', 1);
INSERT INTO io_record VALUES (2, 1, 2.00, 8.00, '2019-12-10 12:14:46', '1', '2131321231', 2);
INSERT INTO io_record VALUES (3, 2, 10.00, 18.00, '2019-12-10 12:51:39', '2', NULL, 3);
INSERT INTO io_record VALUES (4, 1, 10.00, 10.00, '2019-12-11 13:24:42', '1', '123123123', 2);
COMMIT;

image.png
SELECT
    MAX( id ) AS id 
FROM
    io_record 
GROUP BY
    user_s_id,
    DATE_FORMAT(
    add_time,
    '%Y-%m-%d')

可以求出每天每人最后一条的id
再关联自己得出信息

上一篇下一篇

猜你喜欢

热点阅读