0

我有两张桌子,正如标题所说,我需要像双内连接这样的东西。我不知道这是否有效,但我相信应该有一个简单的方法。

我得到的是这个工作正常的声明:

SELECT 
    t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
    t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink
FROM images t1
INNER JOIN navigation t2
ON t2.id=t1.nav_id 
ORDER BY RAND() LIMIT 0,101

现在 t2(导航表)看起来像这样

+----+-----------+------------------+------------------+------+
| id | parent_id | name             | directlink       | rang |
+----+-----------+------------------+------------------+------+
|  1 |         0 | Home             | home             |    0 |
|  3 |         0 | Architektur      | architektur      |    1 |
|  7 |         0 | Design           | design           |    2 |
|  8 |         0 | Contact          | contact          |    3 |
| 11 |         3 | Surfabricaziun 5 | surfabricaziun_5 |    0 |
| 12 |         7 | Fluor            | fluor            |    1 |
| 13 |         7 | Maisa            | maisa            |    2 |
| 14 |         3 | Fuldera          | fuldera          |    3 |

和 t1 (图像表)看起来像这样

+-----+--------+------+----------------------+-----------+----------+
| id  | nav_id | rang | name                 | img_title | img_text |
+-----+--------+------+----------------------+-----------+----------+
| 700 |     11 |   80 | Siedlg_aussen_26.jpg |           |          |

我从 sql 语句得到的输出是:

+--------+------------+-------------+-----------+----------+--------+---------------+----------+------------------+
| img_id | img_nav_id | img_name    | img_title | img_text | nav_id | nav_parent_id | nav_name | nav_directlink   |
+--------+------------+-------------+-----------+----------+--------+---------------+----------+------------------+
|    625 |         11 | 07.jpg      | 11        |          |     11 |             3 | Surfabri | surfabricaziun_5 |
|    744 |         20 | 85.jpg      |           |          |     20 |             7 | Test     | test             |

现在我想要或需要的是:我需要获取父导航名称。所以我想要一个名为 nav_parent_name 的字段where t2.parent_id = t2.id,为此我尝试过

SELECT 
    t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
    t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink,
    t2.name nav_parent_name
FROM images t1
INNER JOIN navigation t2
INNER JOIN navigation t2
    ON t2.parent_id = t2.id AS nav_parent_name
ON t2.id=t1.nav_id 
ORDER BY RAND() LIMIT 0,101

这是行不通的。问题:我对连接一无所知是否可以获得我想要的结果,或者我是否必须编写一个新的 Sql 语句,这很容易,但我希望只有一个语句适用于我需要的所有数据。

提前感谢大家阅读的任何建议和建议。

4

1 回答 1

1

您有一些别名问题,需要在添加新连接时保留现有连接

SELECT 
    t1.id img_id, t1.nav_id img_nav_id, t1.name img_name, t1.img_title img_title, t1.img_text img_text,
    t2.id nav_id,t2.parent_id nav_parent_id, t2.name nav_name, t2.directlink nav_directlink,
    t3.name nav_parent_name
FROM images t1
INNER JOIN navigation t2
  ON t2.id=t1.nav_id
INNER JOIN navigation t3
    ON t2.parent_id = t3.id
ORDER BY RAND() LIMIT 0,101
于 2013-10-13T13:12:06.647 回答