我從兩個不同的查詢創建的查詢的結果不會返回相同的結果。組合查詢返回不同的結果
查詢
SELECT
c.fullname Course,
u.firstname First,
u.lastname Last,
u.id ID,
u.institution Company
FROM (mdl_scorm_scoes_track AS st)
JOIN mdl_user AS u ON st.userid=u.id
JOIN mdl_scorm AS sc ON sc.id=st.scormid
JOIN mdl_course AS c ON c.id=sc.course
Join mdl_user_enrolments AS uenr ON uenr.userid=u.id
Join mdl_enrol AS enr ON enr.id=uenr.enrolid
WHERE (
(st.value='incomplete' OR st.value='not attempted')
AND DATEDIFF(NOW(), FROM_UNIXTIME(uenr.timecreated)>60)
ORDER BY c.fullname, u.lastname,u.firstname, u.id
查詢乙
SELECT
c.fullname AS Course,
u.firstname AS Firstname,
u.lastname AS Lastname,
u.id AS ID,
u.institution AS Company,
IF (u.lastaccess = 0,'never',
DATE_FORMAT(FROM_UNIXTIME(u.lastaccess),'%Y-%m-%d')) AS dLastAccess
,(SELECT DATE_FORMAT(FROM_UNIXTIME(timeaccess),'%Y-%m-%d') FROM mdl_user_lastaccess WHERE userid=u.id AND courseid=c.id) AS CourseLastAccess
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS e ON e.id = ue.enrolid
JOIN mdl_course AS c ON c.id = e.courseid
JOIN mdl_user AS u ON u.id = ue.userid
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = u.id
WHERE ul.timeaccess IS NULL AND (DATEDIFF(NOW(), FROM_UNIXTIME(ue.timecreated))>60)
ORDER BY u.id, c.fullname
我已經它們合併成查詢Ç
SELECT
c.fullname AS Course,
u.firstname AS Firstname,
u.lastname AS Lastname,
u.id AS IDNumber,
u.institution AS Institution,
IF (u.lastaccess = 0,'never',
DATE_FORMAT(FROM_UNIXTIME(u.lastaccess),'%Y-%m-%d')) AS dLastAccess
,(SELECT DATE_FORMAT(FROM_UNIXTIME(timeaccess),'%Y-%m-%d') FROM mdl_user_lastaccess WHERE userid=u.id AND courseid=c.id) AS CourseLastAccess
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS e ON e.id = ue.enrolid
JOIN mdl_course AS c ON c.id = e.courseid
JOIN mdl_user AS u ON u.id = ue.userid
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = u.id
WHERE (ul.timeaccess IS NULL OR ue.userid IN
(SELECT u.id
FROM (mdl_scorm_scoes_track AS st)
JOIN mdl_scorm AS sc ON sc.id=st.scormid
WHERE c.id=sc.course AND st.userid=u.id AND (st.value='incomplete' OR st.value='not attempted')
)
)AND (DATEDIFF(NOW(), FROM_UNIXTIME(ue.timecreated))>60)
ORDER BY c.fullname, u.lastname,u.firstname
我還沒有發現在我的邏輯是在查詢C.查詢不正確C會添加A或B未找到的錯誤記錄,並在幾個情況下複製條目。
我想了解一下我的邏輯組合2的方式走向歧途。
我在這篇文章中修正了逗號,實際的查詢確實有這些逗號。
我的意圖是列出所有已登記進入課程但在給定時間範圍內的用戶,尚未登錄系統,訪問課程並最終沒有完成課程中的活動。
所以我要尋找的邏輯是: 如果60天之內用戶沒有登錄 - 顯示名稱 如果登錄,但一直沒有在60天內訪問過課程 - 顯示名稱 如果登錄並訪問查詢A確實列出所有在60天內開始活動但尚未完成的用戶 查詢B確實列出了所有未登錄或訪問課程的用戶60天
在試圖結合2個查詢來滿足所有3個條件離子是我遇到問題的地方。我第一次嘗試了一個聯盟,但無法實現它的工作。
您錯過了查詢A的第二行和查詢B的第六行的逗號。 – Rachcha
@Rachcha - 感謝您指出缺少的逗號。我已更正帖子,但實際查詢是正確的。 –