sql left join on


1  查詢字段數據,可以直接使用字段別名查詢。

SELECT 系統, ob_id AS 服務器, 憑證個數, 使用憑證, 使用時長(秒)
, 使用次數, 最后使用人, 最后使用時間
FROM ( SELECT ra_target_resource_id, ra_credential_id AS 使用憑證 , SUM(TIMESTAMPDIFF(SECOND, ra_start_time, ra_end_time)) AS 使用時長(秒) , COUNT(ra_credential_id) AS 使用次數, gs_requester_id AS 最后使用人 , MAX(ra_start_time) AS 最后使用時間 FROM am_ucmrecordedapp

2  usageLog   虛擬表 別名 as 別名 (as可以去掉)

am_ucmrecordedapp    左表
LEFT JOIN
am_ucmgwsession
左連接 查詢

1、 on條件是在生成臨時表時使用的條件,它不管on中的條件是否為真,都會返回左邊表中的記錄。

2、where條件是在臨時表生成好后,再對臨時表進行過濾的條件。這時已經沒有left join的含義(必須返回左邊表的記錄)了,條件不為真的就全部過濾掉



SELECT 系統, ob_id AS 服務器, 憑證個數, 使用憑證, 使用時長(秒)
, 使用次數, 最后使用人, 最后使用時間
FROM (
SELECT ra_target_resource_id, ra_credential_id AS 使用憑證
, SUM(TIMESTAMPDIFF(SECOND, ra_start_time, ra_end_time)) AS 使用時長(秒)
, COUNT(ra_credential_id) AS 使用次數, gs_requester_id AS 最后使用人
, MAX(ra_start_time) AS 最后使用時間
FROM am_ucmrecordedapp
LEFT JOIN am_ucmgwsession ON ra_short_session_id = gs_short_session_id
WHERE (ra_credential_id != 'null'
AND ra_end_time != 'null'
AND ra_start_time > '2018-01-01'
AND ra_end_time < '2018-12-31')
GROUP BY ra_credential_id
) usageLog
LEFT JOIN (
SELECT ob_description AS 系統, ob_id, COUNT(cr_targetrsc_uuid) AS 憑證個數
FROM am_object
LEFT JOIN am_ucmcred ON cr_targetrsc_uuid = ob_uuid
WHERE ob_object_class = 'PasswordTargetResource'
GROUP BY cr_targetrsc_uuid
ORDER BY ob_description
) connInfo
ON usageLog.ra_target_resource_id = connInfo.ob_id
WHERE 系統 = 'centos'
ORDER BY connInfo.系統;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM