SELECT
S.carOwnerID ,
S.name,
S.mobile,
S.coopName,
S.VIN,
s.policyNO,
S.effectiveDate,
S.expiryDate,
s.plateNo,
(
CASE
WHEN s.num > 1 THEN
1
WHEN s.num = 1 THEN
0
END
) AS carState
FROM
(
SELECT
c.carOwnerID,
c.name,
c.mobile,
c.coopName,
c.VIN,
p.policyNO,
p.effectiveDate,
p.expiryDate,
c.plateNo,
count(p.PlateNo) AS num
FROM
customer C
LEFT JOIN policy P ON C.carOwnerID = P.carOwnerID
WHERE
date_add(
P.createTime,
INTERVAL 11.5 HOUR
) > NOW()
) s
WHERE
s.num > 0;
簡單的用多表關聯是可以的,如果遇到高並發,效能缺陷立即就出來
多表關聯並不代表sql高效能,太複雜的SQL效率損耗也不比外部鎖等低,跟高並發也沒什麼衝突。
建議把語句拆成簡單語句,使用鎖和補償機制來保障事務性。