MySQL 8.0.27 Left Join 一個子查詢的問題?


以下代碼,a left join b 能查詢到數據,c 結果集單獨查詢無數據。
現在出現了一個詭異的問題:整個 SQL 查詢 c.submit_id 既然有數據,應該為 NULL 才對。
如果將 SELECT t1.*, t2.input_name,t2.sort_number 加一個 DISTINCT 就正常了。

SELECT
 a.user_id AS userId,
 b.org_name AS deptName,
 c.submit_id AS submitId,
FROM
 USER a
 LEFT JOIN org b ON a.org_id = b.org_id
 LEFT JOIN (
   SELECT t1.*, t2.input_name,t2.sort_number
   FROM questionnaire_result t1 
   JOIN template_form t2 ON t1.input_id = t2.input_id
   WHERE EXISTS (select 1 from questionnaire_result x where t1.submit_id = x.submit_id and x.questionnaire_id = 'q1' and x.is_deleted= 0 and x.input_id = 't1f1' and x.content_name = '2022-03-241')
 ) c ON a.user_id = c.creator_id
WHERE a.is_deleted = 0

前后執行 explain 對比:

PS:在 10.5.5-MariaDB 沒有問題。


免責聲明!

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



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