先不说话,来两个运行结果看看
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