mysql - LEFT JOIN - losing a line from LEFT table selection -
i have trouble following query:
select ifnull(t4.itemid, ifnull(t3.itemid, ifnull(t2.itemid, t1.itemid))) id, ifnull(t4.parentitemid, ifnull(t3.parentitemid, ifnull(t2.parentitemid, t1.parentitemid))) parent, ifnull(tp4.itemno, ifnull(tp3.itemno, ifnull(tp2.itemno, tp1.itemno))) itemno itemsparents t1 left join itemsdb tp1 on t1.itemid = tp1.id left join itemsparents t2 on t1.itemid = t2.parentitemid left join itemsdb tp2 on t2.itemid = tp2.id left join itemsparents t3 on t2.itemid = t3.parentitemid left join itemsdb tp3 on t3.itemid = tp3.id left join itemsparents t4 on t3.itemid = t4.parentitemid left join itemsdb tp4 on t4.itemid = tp4.id t1.parentitemid = ( select id itemsdb itemnoint = 359 )
this table should return 2 rows (822 , 875) first part of selection , append more rows following itemsparents left joins
. appends new rows, 1 row original 2 gets lost.
822
859
834
846
810
...so the row 875 got lost.
if remove or incapacitate left join
parent items (by "= 1 -- "
- commenting out original condition):
select ifnull ( t4.itemid, ifnull(t3.itemid, ifnull(t2.itemid, t1.itemid))) id, ifnull(t4.parentitemid, ifnull(t3.parentitemid, ifnull(t2.parentitemid, t1.parentitemid))) parent, ifnull(tp4.itemno, ifnull(tp3.itemno, ifnull(tp2.itemno, tp1.itemno))) itemno itemsparents t1 left join itemsdb tp1 on t1.itemid = tp1.id left join itemsparents t2 on t1.itemid = 1 -- t2.parentitemid left join itemsdb tp2 on t2.itemid = tp2.id left join itemsparents t3 on t2.itemid = 1 -- t3.parentitemid left join itemsdb tp3 on t3.itemid = tp3.id left join itemsparents t4 -- t4.parentitemid on t3.itemid = 1 left join itemsdb tp4 on t4.itemid = tp4.id t1.parentitemid = ( select id itemsdb itemnoint = 359 )
...i returns correct 2 rows left joined table (822 , 875).
i googled cases of lost rows in left joined table, none of them seems apply case. idea?
regards,
libor
not answer idea, hope have asked finding last row of table in left join,
select lastrowofsubtable.* tablemain outer apply ( select top 1 * tablesub tablemain.id = tablesub.id order tablesub.pk_id desc )as lastrowofsubtable
Comments
Post a Comment