-- SELECT count(*) FROM mydb.member where u_business = 1 and status = 1

SELECT a.account_name as '이름', a.nick_name as '닉네임', a.call as '전번', a.account_number as '계좌',
		case
			when status = 1
				then '정상'
            when status = 2
				then '정지'
            when status =  3
				then '탈퇴'
            when status =  11
				then '대기'
			end as '상태'
        , IFNULL((SELECT SUM(c.money) FROM member_money_charge_history c WHERE c.member_idx=a.idx AND c.status=3),0) AS '입금' 
        -- , IFNULL((SELECT SUM(d.money) FROM member_money_exchange_history d WHERE d.member_idx=a.idx AND d.status=3),0) AS '출금'
        , (
            IFNULL((SELECT SUM(c.money) FROM member_money_charge_history c WHERE c.member_idx=a.idx AND c.status=3), 0)
            - 
            (IFNULL((SELECT SUM(d.money) FROM member_money_exchange_history d WHERE d.member_idx=a.idx AND d.status=3),0) + a.money)
            ) as '정산'
        FROM member a
        where u_business = 1 and level != 9;