在這裏,我用左連接表造船廠編寫了一個查詢。沒有與此表相關的連接,但它在加入或刪除時影響結果,問題是爲什麼? 主要影響OrderBook和TotalShips列。爲什麼影響左連接表造船廠的結果
select a.sbwynum,
a.sbnam,
a.deleted,
sum(if ((sh.statuscod = 'O' or sh.statuscod = 'S') and (left (
condeldat, 4) = '2011' or left (adjdeldat, 4) = '2011' or left (
deldat, 4) = '2011'), sh.cgt, 0)) as CurrCgt,
count(if ((sh.statuscod = 'O' or sh.statuscod = 'S') and (left (
condeldat, 4) = '2011' or left (adjdeldat, 4) = '2011' or left (
deldat, 4) = '2011'), 1, NULL)) as CurrShips,
count(if (sh.statuscod = 'O', 1, NULL)) as OrderBook,
count(if (sh.statuscod = 'S', 1, NULL)) as TotalShips,
a.country as coucod,
ct.counam,
a.fulnam,
a.status,
a.stoclist,
if (sh.statuscod = 'O', 1, 2) as StatusFlag
from shipbuilder as a
left join
(select sbwynum, statuscod, condeldat, adjdeldat, deldat, cgt from
`ship` s join shiptype st on s.wytypid = st.wytypid and st.forsearch
= 'Y' and st.searchsb = 'Y' and deleted = 'N') sh on sh.sbwynum =
a.sbwynum
left join country ct on ct.coucod = a.country and ct.deleted = 'N'
left join shipyard sy on a.sbwynum = sy.sbwynum and sy.deleted != 'Y' and
sy.syclsid != 'B'
where a.sbwynum != '' and
a.deleted = 'N' and
a.status != 'FV' and
a.country = '365'
group by a.sbwynum
having a.deleted = 'N'
order by sbnam
非常感謝.....
內如何當它包含它影響到它,少結果或多個結果被應用?你有沒有嘗試過SELECT DISTINCT,看看是否仍然給出不同的結果?有幾個原因可能會導致這種情況發生: – 2011-06-08 05:48:48
當我刪除其獲取更少的訂單和總計,反之亦然。 – Bajrang 2011-06-08 05:52:50