i have following (much simplified) view:
select t1.item, t2.text t1 left outer join t2 on t1.code = t2.code , t1.item = t2.item (t1.item = '12345') , (t1.code = '678') , (t2.detailid = '99')
t1
╔═══════╦═══════╦═══════════════╗ ║ code ║ item ║ other info... ║ ╠═══════╬═══════╬═══════════════╣ ║ code1 ║ item1 ║ other info... ║ ║ code2 ║ item2 ║ other info... ║ ╚═══════╩═══════╩═══════════════╝
t2
╔═══════╦═══════╦═══════════╦═══════╦═══════════════╗ ║ code ║ item ║ detailid ║ text ║ other info ║ ╠═══════╬═══════╬═══════════╬═══════╬═══════════════╣ ║ code1 ║ item1 ║ detailid1 ║ text1 ║ other info... ║ ║ code1 ║ item1 ║ detailid2 ║ text2 ║ other info... ║ ║ code1 ║ item1 ║ detailid3 ║ text3 ║ other info... ║ ║ code2 ║ item2 ║ detailid1 ║ text4 ║ other info... ║ ╚═══════╩═══════╩═══════════╩═══════╩═══════════════╝
nb: detailid
reused on multiple codes/items
in instances works fine. except on occasions, detailid
99 not exist code , item. when happens, returns 0 rows (i thought left outer join
fix sql amateurish). how can return row, just
12345, ''
as result in instance?
you need move condition on second table on
clause:
select t1.item, t2.text t1 left outer join t2 on t1.code = t2.code , t1.item = t2.item , t2.detailid = '99' (t1.item = '12345') , (t1.code = '678') ;
when there no match, value null
, original where
clause filters out.
Comments
Post a Comment