在mysql中,如何使用连接表中的一行来过滤组中不需要的日期?

jbose2ul  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(384)

我有一个 loan 表和a loan_status table。这个 loan 除其他列外,该表还包括 loan_id 和一个 date_issued 列。这里有一个查询 date_issued 对于其他贷款:

(select GROUP_CONCAT(Date(lj.date_issued) SEPARATOR ', ') from loan lj INNER JOIN client clj ON (clj.client_id = lj.client_id) 

where clj.client_id = '2299629' and lj.id != l.id) as other_loan_issue_dates,

因为这个客户有7笔贷款,所以我有一个这样的表格,有6个逗号分隔的日期:

问题是:
我需要排除在 other_loan_issue_dates 任何 loan 有一个 status_id27loan_status table和那个 status_id 是最后一个进入 loan_status 那张table loan . 每笔贷款都有许多状态。
举个例子:

此查询显示 test 列,该列确定是否应在 other_loan_issue_dates :

select status_id = 27 as test from loan_status ls where loan_id = 2052456 ORDER BY datestamp DESC LIMIT 1;

以下错误且未完成运行:

(select GROUP_CONCAT(Date(lj.date_issued) SEPARATOR ', ') from loan lj INNER JOIN client clj ON (clj.client_id = lj.client_id) 

JOIN loan_status ls ON ls.status_id = (
select status_id from loan_status ls where loan_id = 2052456 ORDER BY datestamp DESC LIMIT 1
)

where clj.client_id = '2299629' and lj.id != l.id and ls.status_id != 27) as other_loan_issue_dates,

我也尝试了以下方法,但我不知道它是否有效,因为它似乎使结果成倍增加:

(select GROUP_CONCAT(Date(lj.date_issued) SEPARATOR ', ') from loan lj INNER JOIN client clj ON (clj.client_id = lj.client_id) 

LEFT OUTER JOIN loan_status ls ON (ls.loan_id = lj.id)

where clj.client_id = '2299629' and lj.id != l.id and (select status_id = 27 as test from loan_status ls where loan_id = ls.loan_id ORDER BY datestamp DESC LIMIT 1) = 0) as other_loan_issue_dates,

最后一个怎么用 loan_status 行,检查是否 status_id27 如果是这样的话,排除这个 loan ?

hc2pp10m

hc2pp10m1#

我不确定你的查询是否正确,因为这只是一些更大查询的一部分。不过,我建议您尝试以下方法:

SELECT GROUP_CONCAT(Date(lj.date_issued) SEPARATOR ', ')
FROM loan lj
INNER JOIN client clj ON (clj.client_id = lj.client_id) 
WHERE clj.client_id = '2299629'
AND lj.id != l.id
AND lj.loan_id NOT IN (SELECT loan_id from loan_status WHERE status_id = 27)

这个 SELECT loan_id from loan_status WHERE status_id = 27 会给你贷款的身份证 status_id=27 . 然后你把 NOT IN 告诉mysql排除这些贷款。

相关问题