简体   繁体   English

聚合现有查询以适用于多行

[英]Aggregating existing query to work for multiple rows

I have a ledger table, and right now I have the ability to find the date or NULL if someone is delinquent based on their payment history. 我有一个分类帐表,现在我有能力找到日期或NULL,如果有人根据他们的付款记录拖欠。 I need a query that allows me to find all delinquent members instead of just a specific one. 我需要一个查询,允许我找到所有逾期成员而不是特定成员。

I need the ability to run a query that gets any member that is delinquent and return to me the member_id and the date of delinquency. 我需要能够运行一个查询,获取任何拖欠的成员并返回给我member_id和拖欠日期。

Basically what the original query to find delinquency for a specific member does, just doing every member instead of a specific one. 基本上是为特定成员找到拖欠的原始查询,只做每个成员而不是特定成员。

I have tried: 我努力了:

SELECT DISTINCT member_id, created_at FROM member_ledger_items WHERE 
balance > 0 and id > (
    IFNULL(
        (SELECT id from member_ledger_items WHERE balance <= 0 and member_ledger_items.deleted_at is NULL GROUP BY member_id ORDER BY created_at, id desc LIMIT 1),
        0
    )
) and `member_ledger_items`.`deleted_at` is null GROUP BY member_id order by created_at asc, id asc;

This is the query to find if a specific member is delinquent: 这是查找特定成员是否拖欠的查询:

select `created_at` from `member_ledger_items` where `member_id` = ? and `balance` > 0 and `id` > 
(
IFNULL(
(select `id` from `member_ledger_items` where `member_id` = ? and `balance` <= 0 and `member_ledger_items`.`deleted_at` is null order by `created_at` desc, `id` desc limit 1)
, 0)
) 
and `member_ledger_items`.`deleted_at` is null order by `created_at` asc, `id` asc limit 1;

Here is the create syntax of the member_ledger_items table: 以下是member_ledger_items表的创建语法:

CREATE TABLE `member_ledger_items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `member_id` int(10) unsigned NOT NULL,
  `status` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `type` enum('credit','debit') COLLATE utf8_unicode_ci NOT NULL,
  `category` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `memo` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `amount` decimal(13,3) DEFAULT NULL,
  `autopay` tinyint(1) DEFAULT NULL,
  `late` tinyint(1) DEFAULT NULL,
  `date` date NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `balance` decimal(13,3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=53596 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I need rows with member_id and date of starting delinquency. 我需要有member_id的行和开始拖欠的日期。

Is this possible? 这可能吗?

Any help would be appreciated! 任何帮助,将不胜感激!

SELECT `member_id`, 
       (SELECT `created_at` 
        FROM   `member_ledger_items` AS MLI2 
        WHERE  `balance` > 0 
               AND MLI2.`member_id` = MLI.`member_id` 
               AND `id` > ( Ifnull((SELECT `id` 
                                    FROM   `member_ledger_items` AS MLI3 
                                    WHERE  `balance` <= 0 
                                           AND MLI3.`member_id` = 
                                               MLI2.`member_id` 
                                           AND MLI3.`deleted_at` IS NULL 
                                    ORDER  BY `created_at` DESC, 
                                              `id` DESC 
                                    LIMIT  1), 0) ) 
               AND MLI2.`deleted_at` IS NULL 
        ORDER  BY `created_at` ASC, 
                  `id` ASC 
        LIMIT  1) AS created_date 
FROM   `member_ledger_items` AS MLI 
GROUP  BY `member_id`; 

Ended up being the solution 结束了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM