MySQL查询结果返回意外数据

时间:2013-01-21 03:40:45

标签: mysql sql

我正在尝试运行一个自定义报告的查询,该报告应该根据指定日期范围内的贷款还款类型为符合五个条件的客户返回数据。标准为 Salary Deduction Bank Standing Order Self-Pay Post Dated Cheques Bank Debit 。结果应返回指定的每个条件的客户端数量。但是,这不是我目前得到的结果;正在针对此软件Mambu测试这些查询。结果取决于 customfield customfieldvalue 指定要使用的还款类型)列。这就是我期望得到的结果:

++++++++++++++++++++++++++++++++++++++++++++++++++++++
| LoanProduct           | RepaymentType    | Clients |
++++++++++++++++++++++++++++++++++++++++++++++++++++++
| JUMPSTART LOAN WEEKLY | Self-Pay         | 35      |
------------------------------------------------------
| PAYDAY LOAN MONTHLY   | Salary Deduction | 5       |
------------------------------------------------------
| MICRO-BIZ LOAN        | Bank Debit       | 26      |
------------------------------------------------------
| PAYDAY LOAN WEEKLY    | Self-Pay         | 1       |

  .......
and so on ...

到目前为止我尝试过的解决方案:

QUERY#1:
对于这个查询,我只是试图找回所有客户端而不计算特定的还款类型;它适用于那种情况。

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE = "Bank Debit"
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

QUERY#1.1
在上面添加OR不会产生任何结果:

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE = "Bank Debit" OR customfieldvalue.VALUE = "Self-Pay"
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

QUERY#2
我甚至尝试使用CASE语句执行此操作,但它仅将Clients的所有行作为NULL返回,并且需要大约12.1秒才能完成。

SELECT
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType,
    CASE
       WHEN customfieldvalue.VALUE = "Salary Deduction" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Bank Standing Order" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Self-Pay" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Post Dated Cheques" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
       WHEN customfieldvalue.VALUE = "Bank Debit" THEN COUNT(CONCAT(client.FIRSTNAME, ' ', client.LASTNAME))
    END AS Clients
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON  loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

我在这里做错了什么,这使我无法获得理想的结果?提前谢谢。

3 个答案:

答案 0 :(得分:1)

对于查询2,您需要添加括号,或使用in语法:

AND customfieldvalue.VALUE in ('Bank Debit', 'Self-Pay')

与在join子句中修复from语法正确的语法一样重要:

FROM client join
     user
     on client.ASSIGNEDUSERKEY = user.ENCODEDKEY join
     loanproduct
     ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY and
        loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY CROSS JOIN
    customFieldValue
where loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31" and
      customfieldvalue.value in ('Bank Debit')
天哪,你会马上看到有些东西可能是错的。您通常不希望在设计良好的数据仓库中交叉连接表。 CustomFieldValue如何连接到其他表?

答案 1 :(得分:0)

报价(操作顺序)在这里非常重要,你错过了它们,这就是问题所在。

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND (customfieldvalue.VALUE = "Bank Debit" OR customfieldvalue.VALUE = "Self-Pay")
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

替代方案是使用“IN()”

SELECT CONCAT(client.FIRSTNAME, ' ', client.LASTNAME) AS Client, 
    CONCAT(user.FIRSTNAME, ' ', user.LASTNAME) AS Originator, 
    loanproduct.PRODUCTNAME AS LoanProduct, customfieldvalue.VALUE AS RepaymentType
FROM client, user, customfieldvalue, loanaccount
INNER JOIN loanproduct ON loanaccount.PRODUCTTYPEKEY = loanproduct.ENCODEDKEY 
WHERE client.ASSIGNEDUSERKEY = user.ENCODEDKEY 
    AND loanaccount.ACCOUNTHOLDERKEY = client.ENCODEDKEY
    AND customfieldvalue.VALUE IN("Bank Debit", "Self-Pay")
    AND loanaccount.DISBURSEMENTDATE BETWEEN "2012-01-01" AND "2013-01-31"
GROUP BY user.LASTNAME, client.LASTNAME

答案 2 :(得分:0)

一些项目脱颖而出。 首先,您似乎尝试加入4个文件,但只定义了1个连接 - 有时在构建复杂连接时有助于添加文件1。

你也在使用GROUP BY而不聚合任何东西 - 你确定这是你想要的吗?如果您想要计数,可以使用count(*),然后所有其他列都需要包含在GROUP BY子句中。