Skip to main content
 首页 » 数据库

mysql 复杂的sql实例

2022年07月18日45三少


SELECT
apply.assets_code,
apply.loan_apply_code,
cust.cust_name,
cust.id_no,
cust.mobile,
platform.platform_name,
product.product_name,
subproduct.product_sub_name,
(
CASE
WHEN assets.assets_type = '1' THEN
'消费分期'
WHEN assets.assets_type = '2' THEN
'物流金融'
WHEN assets.assets_type = '3' THEN
'汽车金融'
WHEN assets.assets_type = '4' THEN
'农村金融'
WHEN assets.assets_type = '5' THEN
'经营周转贷'
WHEN assets.assets_type = '6' THEN
'保险金融'
WHEN assets.assets_type = 7 THEN
'信用分期'
END
) AS assets_type,
apply.loan_amount,
apply.init_period,
FORMAT(apply.contract_rate * 100.0,2) as contract_rate,
(
SELECT
SUM(principal_amt)
FROM
aim_t_loan_repay_process
WHERE
assets_code = apply.assets_code
-- ORDER BY
-- id DESC
-- LIMIT 0,
-- 1
) AS cur_not_balance,
(
CASE
WHEN apply.repayment_type_code=1 then '按月付息,到期还本(按日计)'
WHEN apply.repayment_type_code=2 then '按月付息,到期还本(按自然月计)'
WHEN apply.repayment_type_code=3 then '到期一次性还本付息'
WHEN apply.repayment_type_code=4 then '等额本息'
WHEN apply.repayment_type_code=5 then '等额本金'
END
) as repayment_type_code,
apply.create_time as loan_apply_time,
(
CASE
WHEN apply.approve_status='B001' then '待初审'
WHEN apply.approve_status='B002' then '待终审'
WHEN apply.approve_status='B003' then '人工通过'
WHEN apply.approve_status='B004' then '人工拒绝'
WHEN apply.approve_status='B005' then '系统通过'
WHEN apply.approve_status='B006' then '系统拒绝'
END
) as approve_status,
(
CASE
WHEN apply.apply_status='A001' then '待审核'
WHEN apply.apply_status='A002' then '撤单'
WHEN apply.apply_status='A003' then '初审拒绝'
WHEN apply.apply_status='A004' then '系统拒绝'
WHEN apply.apply_status='A005' then '待准售'
WHEN apply.apply_status='A006' then '复核'
WHEN apply.apply_status='A007' then '销售'
WHEN apply.apply_status='A008' then '放款中'
WHEN apply.apply_status='A009' then '贷中'
WHEN apply.apply_status='A010' then '结案'
WHEN apply.apply_status='A011' then '内部取消'
WHEN apply.apply_status='A013' then '终审拒绝'
WHEN apply.apply_status='A014' then '审核完成'
END
) as apply_status,
audit1.create_time AS first_create_time,
audit1.approver_name AS first_approver_name,
audit1.comments AS first_comments,
audit1.reason_first AS first_reason_first,
audit1.reason_second AS first_reason_second,
audit2.create_time AS second_create_time,
audit2.approver_name AS second_approver_name,
audit2.comments AS second_comments,
audit2.reason_first AS second_reason_first,
audit2.reason_second AS second_reason_second,
(
CASE
WHEN apply.is_open=0 then '未开户'
WHEN apply.is_open=1 then '已开户'
END
) as is_open,
account.create_time AS account_finish_time,

IF (
apply.agreement_confirm_time IS NULL
OR apply.agreement_confirm_time = '',
'未确认',
'已确认'
) AS is_confirm,
apply.agreement_confirm_time,
apply.approve_sale_time,
assets.loan_time,
(
SELECT
repay_date
FROM
aim_t_loan_repay_plan plan
WHERE
plan.assets_code = apply.assets_code
AND plan.deleted = '0'
ORDER BY
id DESC
LIMIT 0,
1
) AS repay_date,
(
CASE
WHEN (
apply.apply_status = 'A014'
AND apply.is_open = 0
) THEN
'待开户'
WHEN (
apply.apply_status = 'A014'
AND (
apply.agreement_confirm_time IS NULL
OR apply.agreement_confirm_time = ''
)
) THEN
'待协议确认'
ELSE
orderstatus.outer_biz_status_name
END
) AS externalStatusInfo,
(
CASE
WHEN (repayAcc.is_settle = 1) THEN (select actual_repayment_date from aim_t_loan_repay_process process
where process.assets_code= assets.assets_code and process.is_settle='1' ORDER BY cur_period desc limit 1)
END
) AS settledDate,
apply.apply_no
FROM
aim_t_loan_apply apply
LEFT JOIN aim_t_outer_inner_status orderstatus ON orderstatus.inner_biz_status_code = apply.apply_status
LEFT JOIN aim_t_cust_base cust ON apply.cust_code = cust.cust_code
LEFT JOIN aim_t_loan_assets assets ON apply.apply_no = assets.apply_no
LEFT JOIN aim_t_loan_repay_account_status repayAcc ON repayAcc.assets_code = assets.assets_code
LEFT JOIN aim_t_assets_platform platform ON apply.assets_platform_code = platform.platform_code
LEFT JOIN aim_t_product product ON apply.product_code = product.product_code
LEFT JOIN aim_t_product_sub subproduct on (product.product_code = subproduct.product_code and apply.loan_period = subproduct.period and subproduct.deleted = '0')
LEFT JOIN aim_t_cust_sub_account account ON (cust.id_no = account.id_no AND cust.id_type = account.id_type)
LEFT JOIN aim_t_risk_third_exception ex ON ex.apply_no = apply.apply_no
LEFT JOIN aim_t_loan_audit audit1 on (audit1.apply_no = apply.apply_no and audit1.result_type=3)
LEFT JOIN aim_t_loan_audit audit2 on (audit2.apply_no = apply.apply_no and audit2.result_type=4)
WHERE
apply.deleted = '0' ORDER BY apply.id limit 1000;


本文参考链接:https://www.cnblogs.com/maohuidong/p/10956007.html