ORACLE 左连接为空
SELECT TSC.ID ID,TSC.USER_ID USER_ID,USER_ID/5 AS GSEND_TIME,TO_CHAR(SEND_TIME,'YYYY-MM-DD HH24:MI:SS') SEND_TIME,CASE WHEN TSC.PRI = 0 THEN '最高' ELSE '普通' END PRI,TSC.CONTENT CONTENT,TO_CHAR(CREATE_TIME,'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,TSC.RESULT RESULT,CASE WHEN TSC.STATUS = 0 THEN '通过' WHEN TSC.STATUS = 1 THEN '不通过' WHEN TSC.STATUS = 2 THEN '审核中' END STATUS,TP.CP_ID CP_ID,TP.PRODUCTNAME PRODUCTNAME,TP.CITY CITY FROM TEL_SMS_CONTENT TSC LEFT JOIN TEL_PRODUCT TP ON TSC.PRODUCT_ID = TP.PRODUCT_ID WHERE TSC.ID =?
运行完后台显示 CP_ID,PRODUCTNAME,CITY为null,其他字段都正常,求大神指教
------解决方案--------------------
引用:SELECT TSC.ID ID,TSC.USER_ID USER_ID,USER_ID/5 AS GSEND_TIME,TO_CHAR(SEND_TIME,'YYYY-MM-DD HH24:MI:SS') SEND_TIME,CASE WHEN TSC.PRI = 0 THEN '最高' ELSE '普通' END PRI,TSC.CONTENT CONTENT,TO_CHAR(CREATE_TIME,'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,TSC.RESULT RESULT,CASE WHEN TSC.STATUS = 0 THEN '通过' WHEN TSC.STATUS = 1 THEN '不通过' WHEN TSC.STATUS = 2 THEN '审核中' END STATUS,TP.CP_ID CP_ID,TP.PRODUCTNAME PRODUCTNAME,TP.CITY CITY FROM TEL_SMS_CONTENT TSC LEFT JOIN TEL_PRODUCT TP ON TSC.PRODUCT_ID = TP.PRODUCT_ID WHERE TSC.ID =?
运行完后台显示 CP_ID,PRODUCTNAME,CITY为null,其他字段都正常,求大神指教
你用了左连接,这样会保证 TEL_SMS_CONTENT 这个表里的记录显示完整,当连接条件不满足时,右表TEL_PRODUCT 中的字段就会为空。
tips: 注意将你贴出的代码格式一下,方便阅读:
SELECT TSC.ID ID,
TSC.USER_ID USER_ID,
USER_ID / 5 AS GSEND_TIME,
TO_CHAR(SEND_TIME, 'YYYY-MM-DD HH24:MI:SS') SEND_TIME,
CASE
WHEN TSC.PRI = 0 THEN
'最高'
ELSE
'普通'
END PRI,
TSC.CONTENT CONTENT,
TO_CHAR(CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
TSC.RESULT RESULT,
CASE
WHEN TSC.STATUS = 0 THEN
'通过'
WHEN TSC.STATUS = 1 THEN
'不通过'
WHEN TSC.STATUS = 2 THEN
'审核中'
END STATUS,
TP.CP_ID CP_ID,
TP.PRODUCTNAME PRODUCTNAME,
TP.CITY CITY
FROM TEL_SMS_CONTENT TSC
LEFT JOIN TEL_PRODUCT TP
ON TSC.PRODUCT_ID = TP.PRODUCT_ID
WHERE TSC.ID = ?