先不說話,來兩個運作結果看看
SELECT
sc.id AS id,
sc.`Name` AS `name`,
sco.`Name` AS companyName,
scs.`Name` AS coldStoreName,
ifnull(tt.flag, 0) AS flag
FROM
sys_monitor_group sc
LEFT JOIN sys_coldstore scs ON sc.CID = scs.id
LEFT JOIN sys_company sco ON scs.company_id = sco.id
LEFT JOIN (
SELECT
sumg.monitor_group_id AS monitorGroupId,
1 AS flag
FROM
sys_user_monitor_group sumg
WHERE
flag != 3
AND user_ID = 245
) tt ON tt.monitorGroupId = sc.id
WHERE
scs.company_id IN (
SELECT
company_id
FROM
sys_user
WHERE
user_ID = 245
)
ORDER BY
sc.ID
SELECT
sc.id AS id,
sc.`Name` AS `name`,
sco.`Name` AS companyName,
scs.`Name` AS coldStoreName,
ifnull(tt.flag, 0) AS flag
FROM
sys_monitor_group sc
LEFT JOIN sys_coldstore scs ON sc.CID = scs.id
LEFT JOIN sys_company sco ON scs.company_id = sco.id
LEFT JOIN (
SELECT
sumg.monitor_group_id AS monitorGroupId,
1 AS flag
FROM
sys_user_monitor_group sumg
WHERE
flag != 3
AND user_ID = 245
) tt ON tt.monitorGroupId = sc.id
WHERE
scs.company_id IN (
SELECT
company_id
FROM
sys_user
WHERE
user_ID = 245
)
兩個sql的差別在于最後的order by語句,但對flag的影響很大,一個是1,一個是0
後續,通過不同資料庫的對比發現是mysql的bug ,資料庫版本号:5.7.20-0ubuntu0.16.04.1