mysql查询计算问题

js81xvg6  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(330)

我有两个表user\u point\u logs和user\u point\u used\u logs,我想得到这样的输出

╔══════╦═════╦══════════════════════╦═══════════════════════╗══════════════╗
║ year ║month║ user_next_expire_date║ user_point_next_expire║minus         ║
╠══════╬═════╬══════════════════════╬═══════════════════════╣══════════════╣
║2018  ║ 10  ║ 2018年10月27日        ║ 200                   ║110          ║
║      ║     ║                      ║                       ║              ║ 
╚══════╩═════╩══════════════════════╩═══════════════════════╝══════════════╝

这里是关系user\u point\u logs.return\u id=user\u point\u used\u logs.id和user\u point\u used\u logs.user\u point\u log\u used\u id=user\u point\u logs.id
用户点日志表

╔════╦════════╦═════╦═══════════╦══════════╗
║ id ║user_id ║point║expire_date║return_id ║
╠════╬════════╬═════╬═══════════╬══════════╣
║  1 ║ 16087  ║ 100 ║1540649153 ║   null   ║
║  2 ║ 16087  ║ 100 ║ null      ║   1      ║
║  3 ║ 16087  ║ 10  ║ null      ║   2      ║
║  4 ║ 16087  ║ 100 ║ 1540649153║   null   ║ 
╚════╩════════╩═════╩═══════════╩══════════╝

表用户\u点\u使用的\u日志:

|-------|-----------------------|-----------------------|------|
| Id    | user_point_log_id     | user_point_log_used_id| point|
|-------|-----------------------|-----------------------|------|
| 1     |         2             |         2             | 100  |
|-------|-----------------------|-----------------------|------|
| 2     |         3             |         3             | 10   |
|-------|-----------------------|-----------------------|------|

我只是试着放置一些样本数据。这是我的问题。

SELECT 
  YEAR(FROM_UNIXTIME(user_point_logs.expire_date, '%Y-%m-%d')) AS year, 
  MONTH(FROM_UNIXTIME(user_point_logs.expire_date, '%Y-%m-%d')) AS month, 
  FROM_UNIXTIME(
    SUBSTRING_INDEX(
      GROUP_CONCAT(
        DISTINCT user_point_logs.expire_date ORDER BY user_point_logs.expire_date ASC
        SEPARATOR ','
      ), 
      ',', 
      1
    ), 
    '%Y年%m月%d日'
  ) AS user_next_expire_date, 
  SUM(user_point_logs.point) AS user_point_next_expire, 
  IF(
    SUM(`user_point_used_logs`.`point`) IS NULL, 
    0, 
    SUM(`user_point_used_logs`.`point`) 
  ) AS `minus` 
FROM `user_point_logs` 
LEFT JOIN (
  SELECT *
  FROM user_point_used_logs
  WHERE user_point_log_id NOT IN (
    SELECT DISTINCT return_id
    FROM user_point_logs
    WHERE return_id IS NOT NULL
  )
) AS user_point_used_logs 
ON (`user_point_logs`.`id` = `user_point_used_logs`.`user_point_log_used_id`) 
WHERE `user_point_logs`.`user_id` = '16087' 
AND `user_point_logs`.`point` > 0
AND `user_point_logs`.`expire_date` > 1540464465 
AND (
  `user_point_logs`.`return_id` = 0 OR 
  `user_point_logs`.`return_id` IS NULL
)
GROUP BY `year`, `month`

问题是,有时计算剩余的点和负得不到实际结果。在我的小提琴中,你会看到用户\u point\u下一个\u expire 200但减0。但是应该有100。
请告诉我我的问题有没有错。
这是我的样本数据集。。。

CREATE TABLE user_point_logs (
  id          int NOT NULL,
  user_id     int NOT NULL,
  point       int(11) NOT NULL,
  expire_date int DEFAULT NULL,
  return_id   int DEFAULT NULL
);

CREATE TABLE user_point_used_logs (
  id                      int NOT NULL,
  user_point_log_id       int NOT NULL,
  user_point_log_used_id  int DEFAULT '0' ,
  point                   int NOT NULL 
);

INSERT INTO user_point_used_logs 
  (id,  user_point_log_id,  user_point_log_used_id, point) 
VALUES 
  (1,   2,                  2,                      100),
  (2,   3,                  3,                      10);

INSERT INTO user_point_logs 
  (id,  user_id,  point,  expire_date,  return_id) 
VALUES
  (1,   16087,    100,    1540649153,   NULL),
  (2,   16087,    100,    NULL      ,   1   ),
  (3,   16087,     10,    NULL      ,   2   ),
  (4,   16083,    100,    1540649153,   NULL),
  (5,   16087,    100,    1540649153,   NULL);

…和同样的小提琴

ajsxfq5m

ajsxfq5m1#

这个问题的问题是,我看不出下面的问题解决不了哪一部分:

SELECT MAX(FROM_UNIXTIME(expire_date)) dt
     , SUM(CASE WHEN expire_date < UTC_TIMESTAMP() THEN point END) a
     , SUM(CASE WHEN expire_date IS NULL THEN point END) b
  FROM user_point_logs
 WHERE user_id = 16087
 GROUP 
    BY user_id;

相关问题