目前,我有一个非常基本的SQL查询,但是我想对其进行扩展,以获取其所引用的ID的名称。问题是,这是“父代”>“子代”关系表。父项和子项是同一表的一部分,但行不同。

    $GetRelations = "
    SELECT RelationUID, BlockTypeUID, ChildBlockUID, Weight FROM block_type_relations
    WHERE BlockTypeUID = '$BlockTypeUID'
    ";


当我只有一个BlockTypeUID来初始获取记录时,如何转移该查询,以便它也从表block_types中获取BlockTypeUID和ChildBlockUID的名称?我在mySQLi中编写此代码。

最佳答案

假设名称在名为“名称”的列中,则查询看起来像这样

$GetRelations = "
SELECT a.RelationUID, a.BlockTypeUID, a.ChildBlockUID, a.Weight
    b.NAME AS BlockTypeName, c.NAME AS ChildBlockName
FROM block_type_relations a
INNER JOIN block_types b ON b.ID = a.BlockTypeUID
INNER JOIN block_types c ON c.ID = a.CildBlockUID
WHERE a.BlockTypeUID = '$BlockTypeUID'
";

10-02 17:45