dqz84361326 2013-01-30 03:34 采纳率: 0%
浏览 51
已采纳

MySQL计数结果

I have a table that is designed like this:

tbl_User
id
Name

tbl_User_Payment
id
user_id
amount
status

tbl_User
id      Name
001     John Doe
002     Juan Dela Cruz


tbl_User_Payment
id        user_id      amount        status
001         001          10         Successful
002         002          10         Fail
003         001          10         Fail
004         001          10         Fail
005         001          10         Fail
006         002          10         Successful
007         002          10         Fail
008         002          10         Fail

The problem here is that how can I count the number of Failed Payment after a Successful Payment. So the result here should be something like this:

John Doe - 3 Failed Payment
Juan Dela Cruz - 2 Failed Payment

I hope you understand what i'm trying to accomplish here.

Your help will be greatly appreciated and rewarded!

Thanks! :)

  • 写回答

4条回答 默认 最新

  • douqian2957 2013-01-30 03:48
    关注

    For brevity and clarity, I'm going to alias a few things in my code.

    SELECT
        Users.UserName,
        FailCount
    FROM
        (
        SELECT
            UserId,
            COUNT(*) As FailCount
        FROM
            Payments,
            (
            SELECT
                UserId,
                MAX( PaymentId ) As LastSuccessId
            FROM
                Payments
            WHERE
                Status = Successful
            GROUP BY
                UserId
            ) AS LastSuccess
        WHERE
            PaymentId > LastSuccessId
        GROUP BY
            UserId
        ) As FailCount
        INNER JOIN Users ON Users.UserId = FailCount.UserId
    ORDER BY
        UserName DESC
    

    I'm a bit rusty, I haven't tested this SQL but if it doesn't work I suspect it's because of my "FROM Payments, <subquery>" syntax. I'm not in a position where I can verify my solution, so you might have to recruit someone to help you if it doesn't work.

    If you're wondering, here's my logic:

    1. My code works in the assumption that PaymentIds are incremented sequentially chronologically.
    2. Do a query that gets the ID of the last successful payment, per user (the innermost SELECT).
    3. Re-query the Payments table to get the COUNT of rows, per user, that come after the "last successful payment ID" values that were retrieved by the innermost SELECT.
    4. JOIN the results from step 3 with the users table to get the usernames.

    Also, a few tips for future database design: there is no need to use Hungarian Notation in DB design - I cringe every time I see "tbl_" prefixes. I also advise against naming primary-key fields simply "id" as it makes JOINs more difficult as you need to disambiguate them. Consider prefixing them with the name of the table (e.g. "payments.id" becomes "Payments.PaymentId").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab