需要更改sql请求

时间:2015-09-08 10:53:44

标签: php mysql json

我有sql请求:

SELECT Bank_ID, Status, COUNT(Bank_ID) as Number 
FROM int_client_bank 
WHERE status = 30 or status = 50 or status = 35 or status = 37 
GROUP BY Bank_ID, Status;

并查看数据:

"Bank_ID"   "Status"    "Number"
"1"         "30"        "772"
"1"         "35"        "58"
"1"         "50"        "151"
"2"         "30"        "124"
"2"         "35"        "27"
"2"         "50"        "25"
"3"         "30"        "227"
"3"         "35"        "16"
"3"         "37"        "1"
"3"         "50"        "143"
"4"         "30"        "337"
"4"         "35"        "23"
"4"         "37"        "1"
"4"         "50"        "98"
"5"         "30"        "72"
"5"         "35"        "7"

{
    "data":[
        {"Bank_Id":"1","Status":"30","Number":"772"},
        {"Bank_Id":"1","Status":"35","Number":"58"},
        ...
    ],
    "success":true
}

我需要更改请求来放置这样的数据:

我需要4种状态--30,35,50,37

 Bank_ID Status30 Status35 Status50 Status37
     1        772       58       151      0
     2        30        27        0       0

{
"data":[
            {"Bank_Id":"1","Status30":"772","Status35":"58","Status50":"151","Status37":",},
{"Bank_Id":"2","Status30":"124","Status35":"27","Status50":"25","Status37":"0"},
...
       ],
       "success":true
       }

我怎么能做json编码或sql请求?

1 个答案:

答案 0 :(得分:0)

您可以使用IF语句结果的SUM来计算状态为特定值的行: -

null pointer exception