我有三个表:

类别:

|Id | TopId|
| 1 | null |
| 2 | null |
| 3 |  1   |
| 4 |  1   |
| 5 |  2   |
| 6 |  3   |


等等...

类别文化:

|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也表示:CategoryId
等等...

文化:

|Id|Name|Flag          |
|1 |en  |/images/en.png|
|2 |de  |/images/de.png|
|3 |tr  |/images/tr.png|


和这个SQL脚本正在得到我所需要的,但没有空的顶部ID。

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


结果是:

mysql - MySQL查询与 parent 属性-LMLPHP

缺少那些TopId为null的行

我已经尝试过很多情况,当针对cp的内部联接进行序列化并尝试了许多脚本时,却无法获得正确的值。

最佳答案

尝试这个

正确加入文化作为文化

关于mysql - MySQL查询与 parent 属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47798917/

10-10 14:14