所以情況是我有一個查詢,涉及9個表,我需要寫它,因此它返回所有記錄,即使當workorderstates
表中的impactid
表是NULL
。mysql左加入9表
上到下面的查詢,我注意到我沒有得到,因爲最初我只是在那裏workorderstates.impactid = impactdefiniton.impactid
並在impactid
是NULL
在workorderstates
表的情況下,這種情況就不會是真實的,是「開放」的所有結果,從而消除因爲實際上「公開」而應該返回的記錄。
所以我設計了下面這個查詢但我每次運行它的時候,它不會工作。它將返回不唯一的表別名workorder
。如果我爲表使用別名,它只會在聯接中的右表上移動,因爲它們不是唯一的。任何人都可以提供任何幫助重組查詢,以便它能工作嗎?我已經嘗試了很多變化和有趣的是第二個查詢ALMOST作品,但它返回重複記錄(在這種情況下,同一個記錄四)
select workorder.workorderid, workorder.siteid,
FROM_UNIXTIME(workorder.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname, IFNULL(workorderstates.impactid, "No Set") as impactid,
IFNULL(impactdefinition.name, "Not Set") as impactname, first_name,
sdorganization.name, statusname, title
from workorder, statusdefinition, sitedefinition, sdorganization,
prioritydefinition, categorydefinition, sduser, aaauser, workorderstates
left Join impactdefinition on workorderstates.impactid = impactdefinition.impactid
left join workorder on workorder.workorderid = workorderstates.workorderid
left join workorderstates on workorderstates.statusid = statusdefinition.statusid
left join workorder on workorder.siteid = sitedefinition.siteid
left join sitedefinition on sitedefinition.siteid = sdorganization.org_id
left join workorderstates on workorderstates.categoryid = categorydefinition.categoryid
left join workorder on workorder.requesterid = sduser.userid
left join sduser on sduser.userid = aaauser.user_id
where statusname='Open' and workorder.createdtime >= '1352678400000'
and sdorganization.name='MAPL'
order by workorder.workorderid
查詢,幾乎工程,但醜(返回重複記錄) :
select workorder.workorderid, workorder.siteid,
FROM_UNIXTIME(workorder.CREATEDTIME/1000, '%m-%d-%Y %H:%i:%s') as createdate,
categoryname, IFNULL(workorderstates.impactid, "No Set") as impactid,
IFNULL(impactdefinition.name, "Not Set") as impactname, first_name,
sdorganization.name, statusname, title
from workorder, statusdefinition, sitedefinition, sdorganization,
prioritydefinition, categorydefinition, sduser, aaauser, workorderstates
left Join impactdefinition on workorderstates.impactid = impactdefinition.impactid
where workorder.workorderid = workorderstates.workorderid
and workorderstates.statusid = statusdefinition.statusid
and workorder.siteid = sitedefinition.siteid
and sitedefinition.siteid = sdorganization.org_id
and workorderstates.categoryid = categorydefinition.categoryid
and workorder.requesterid = sduser.userid and sduser.userid = aaauser.user_id
and statusname='Open' and workorder.createdtime >= '1352678400000'
and sdorganization.name='MAPL'
order by workorder.workorderid
任何想法如何讓這個查詢工作?多謝你們!
'選擇唯一...' ? – 2013-02-17 17:21:35
@JanDvorak'DISTINCT',就是......但你是對的。 – 2013-02-17 17:24:07
將'SELECT DISTINCT'投擲到查詢中以抑制重複行,就像在牆上塗抹模具污漬。 – 2013-02-17 17:37:41