MySQL内连接,其中table2 child_id就像table2 id

时间:2013-10-13 13:09:03

标签: mysql

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

我得到的是这个声明可以正常使用:

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语句,这很容易,但我希望只有一个语句可用于我需要的所有数据。 / p>

提前感谢所有人阅读任何建议和建议。

1 个答案:

答案 0 :(得分: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