在子查询中获取条件计数

时间:2019-01-27 01:22:06

标签: mysql join subquery

我在下面有4张桌子。我需要从contact_made_id表中获取contact_made的计数,以获取'status_loan_id'= 33或43

的记录

我可以使用子查询来执行此操作,但是我既需要获取查询结果中的计数,也需要获取联接中查询中的按计数排序

  1. 贷款
    loan_id | Client_id
    ---------------------
    9727    |   12668
    9780    |   12720
    9781    |   12721
    9782    |   12722
    9783    |   12723
    9784    |   12724
    9785    |   12725
  1. 客户
    Client_id
    ---------------------
    12668
    12720
    12721
    12722
    12723
    12724
    12725
  1. clients_coms
    client_coms_id |   client_id
    -----------------------------
    2114           |   12668 
    2115           |   12668 
    2116           |   12668 
    2117           |   12668  
    2121           |   12668 
    2122           |   12668  
    2260           |   12720 
    2261           |   12720  
    2262           |   12720  
    2263           |   12721  
    2264           |   12721  
    2265           |   12721  
    2266           |   12722  
    2267           |   12722  
    2268           |   12723 
    2269           |   12723  
    2270           |   12723  
    2271           |   12723  
    2272           |   12724  
    2273           |   12724 
    2274           |   12724  
    2275           |   12724 
    2276           |   12725 
    2277           |   12725  
    2278           |   12725  
  1. contact_made
    contact_made_id | loan_id | status_loan_id
    1               | 9727    | 3  
    2               | 9727    | 3  
    3               | 9727    | 34   
    4               | 9727    | 33  
    5               | 9727    | 3 
    6               | 9727    | 33 
    9               | 9727    | 3 
    0               | 9727    | 3 
    11              | 9782    | 33 
    12              | 9782    | 3 
    13              | 9782    | 33 
    14              | 9782    | 3 
    15              | 9782    | 34 

我有以下SQL,但是它给出了以下错误输出

SELECT 
l.loan_id, 
COUNT(cm.contact_made_id) AS contact_count
FROM loans l
LEFT JOIN contact_made cm 
          ON l.loan_id = cm.loan_id 
          AND (cm.status_loan_id = 33 OR cm.status_loan_id = 34)
LEFT JOIN clients_coms com 
          ON l.client_id = com.client_id
GROUP BY l.loan_id
ORDER BY contact_count ASC

输出...

loan_id | contact_count     
------------------------
9780    | 0
9781    | 0
9783    | 0
9784    | 0
9785    | 0
9782    | 6
9727    | 18

应该输出...

loan_id | contact_count     
------------------------
9780    | 0
9781    | 0
9783    | 0
9784    | 0
9785    | 0
9782    | 2
9727    | 3

2 个答案:

答案 0 :(得分:1)

您快到了。

为避免重复,您可以简单地使用COUNT(DISTINCT...),例如:

COUNT(DISTINCT cm.contact_made_id) AS contact_count

但是就您而言,您不需要JOIN clients_coms,因为loan_idcontact_made表中都可以使用loans。删除此联接可避免重复,因此需要使用DISTINCT。我还将OR的{​​{1}}条件更改为loan_id条件。

IN

收益:

| loan_id | contact_count |
| ------- | ------------- |
| 9780    | 0             |
| 9781    | 0             |
| 9783    | 0             |
| 9784    | 0             |
| 9785    | 0             |
| 9727    | 3             |
| 9782    | 3             |

Demo on DB Fiddle

答案 1 :(得分:-1)

您可以在where条件下运行它吗?

SELECT 
l.loan_id, 
COUNT(cm.contact_made_id) AS contact_count
FROM loans l
LEFT JOIN contact_made cm 
          ON l.loan_id = cm.loan_id 
LEFT JOIN clients_coms com 
          ON l.client_id = com.client_id
WHERE cm.status_loan_id in (33, 34)
GROUP BY l.loan_id
ORDER BY contact_count ASC

如果没有,请尝试“内部联接+在哪里”条件