I have three tables:
categories:
|Id | TopId|
| 1 | null |
| 2 | null |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 3 |
etc...
categorycultures:
|Id|CultureOf|CultureId|Name |
|1 | 1 | 1 |Category 1|
|2 | 1 | 2 |Klasse 1|
|3 | 1 | 3 |Kategori 1|
|4 | 2 | 1 |Category 2|
|5 | 2 | 2 |Klasse 2|
|6 | 2 | 3 |Kategori 2|
CultureOf means also: CategoryId etc...
cultures:
|Id|Name|Flag |
|1 |en |/images/en.png|
|2 |de |/images/de.png|
|3 |tr |/images/tr.png|
and this sql script is getting what I need but without null top ids.
select c.Id as Id, cp.Id as TopId, cc.Name as Name, ccp.Name as TopName, cul.Id as CultureId, cul.Name as CultureName
from categories as c
inner join categories as cp on c.TopId = cp.Id
inner join categorycultures as cc on cc.CultureOf = c.Id
inner join categorycultures as ccp on ccp.CultureOf = cp.Id
inner join cultures as cul on cul.Id = cc.CultureId and cul.Id = ccp.CultureId
where cul.Id = 2
results are :
there are missing rows which those TopId's are null
I've tried lot of case when series on inner join for cp and tried many scripts but I cant get the correct values.