一、查询SQL
SELECT
t1.规则编号 AS 编码,
t1.规则描述 AS 名称,
SUM( CASE WHEN t3.DATA_SOURCES = '00' THEN 1 ELSE 0 END ) AS '类型01',
SUM( CASE WHEN t3.DATA_SOURCES = '01' THEN 1 ELSE 0 END ) AS '类型02',
SUM( CASE WHEN t3.DATA_SOURCES = '02' THEN 1 ELSE 0 END ) AS '类型03',
SUM( CASE WHEN t3.DATA_SOURCES = '03' THEN 1 ELSE 0 END ) AS '类型04'
FROM
(
SELECT
'A_M_0001' AS 规则编号,
'规则01' AS 规则描述 UNION ALL
SELECT
'A_M_0002' AS 规则编号,
'规则02' AS 规则描述 UNION ALL
SELECT
'A_M_0003' AS 规则编号,
'规则03' AS 规则描述 UNION ALL
SELECT
'A_M_0005' AS 规则编号,
'规则04' AS 规则描述 UNION ALL
SELECT
'A_M_0007' AS 规则编号,
'规则05' AS 规则描述 UNION ALL
SELECT
'A_M_0006' AS 规则编号,
'规则06' AS 规则描述 UNION ALL
SELECT
'A_M_0008' AS 规则编号,
'规则07' AS 规则描述 UNION ALL
SELECT
'A_J_0001_01' AS 规则编号,
'规则08' AS 规则描述 UNION ALL
SELECT
'A_J_0001_12' AS 规则编号,
'规则09' AS 规则描述 UNION ALL
SELECT
'A_J_0001_02' AS 规则编号,
'规则10' AS 规则描述 UNION ALL
SELECT
'A_J_0001_03' AS 规则编号,
'规则11' AS 规则描述 UNION ALL
SELECT
'A_J_0001_13' AS 规则编号,
'规则12' AS 规则描述 UNION ALL
SELECT
'A_J_0001_05' AS 规则编号,
'规则13' AS 规则描述 UNION ALL
SELECT
'A_J_0001_11' AS 规则编号,
'规则14' AS 规则描述 UNION ALL
SELECT
'A_J_0001_06' AS 规则编号,
'规则15' AS 规则描述 UNION ALL
SELECT
'A_J_0001_14' AS 规则编号,
'规则16' AS 规则描述 UNION ALL
SELECT
'A_J_0001_07' AS 规则编号,
'规则17' AS 规则描述 UNION ALL
SELECT
'A_J_0001_15' AS 规则编号,
'规则18' AS 规则描述 UNION ALL
SELECT
'A_J_0002_01' AS 规则编号,
'规则19' AS 规则描述 UNION ALL
SELECT
'A_J_0002_02' AS 规则编号,
'规则20' AS 规则描述 UNION ALL
SELECT
'A_J_0002_03' AS 规则编号,
'规则21' AS 规则描述 UNION ALL
SELECT
'A_J_0002_04' AS 规则编号,
'规则22' AS 规则描述 UNION ALL
SELECT
'A_J_0002_05' AS 规则编号,
'规则23' AS 规则描述 UNION ALL
SELECT
'A_J_0002_06' AS 规则编号,
'规则24' AS 规则描述 UNION ALL
SELECT
'A_J_0002_07' AS 规则编号,
'规则25' AS 规则描述 UNION ALL
SELECT
'A_J_0003_01' AS 规则编号,
'规则26' AS 规则描述 UNION ALL
SELECT
'A_J_0003_02' AS 规则编号,
'规则27' AS 规则描述 UNION ALL
SELECT
'A_J_0003_05' AS 规则编号,
'规则28' AS 规则描述
) t1
LEFT JOIN RAMS_TRIAL_CHECKLIST t2 ON t2.RULE_CODE like concat('%',t1.规则编号,'%')
LEFT JOIN RAMS_TRIAL_CHECKLIST_EXT t3 ON t2.CHECKLIST_ID = t3.CHECKLIST_ID
WHERE
DATE( t2.UPDATE_TIME ) = CURDATE( ) - INTERVAL 1 DAY
GROUP BY t1.规则编号,t1.规则描述;
二、查询结果
三、总结
1.数据库表中不存在的字段,可以利用以下sql进行处理:
SELECT '60019311' AS code, '北京' AS name
union all
SELECT '60019312' AS code, '上海' AS name
union all
SELECT '60019313' AS code, '广州' AS name
union all
SELECT '60019314' AS code, '重庆' AS name
2.量表关联查询,利用【Like】进行条件关联:
RAMS_TRIAL_CHECKLIST t2 ON t2.RULE_CODE like concat('%',t1.规则编号,'%')
3.case when sql语句:
CASE WHEN t3.DATA_SOURCES = '00' THEN 1 ELSE 0 END
4.查询系统当前时间的前一天数据的数量:
SELECT COUNT(ID) FROM DATA WHERE DATE( UPDATE_TIME ) = CURDATE( ) - INTERVAL 1 DAY