gpt4 book ai didi

mysql - 基于第二个表 ID 从第三个表字段返回值

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:38 25 4
gpt4 key购买 nike

我有这张表

添加 (1)

+----+--------------+--------------+
| id | name of add | Date |
+----+--------------+--------------+
| 1 | Add01 | March 01 |
| 2 | Add02 | March 02 |
| 3 | Add03 | March 03 |
| 4 | Add04 | March 04 |
+----+--------------+--------------+

添加类型 (2)

+----+----------+
| id | Add id |
+----+----------+
| 21 | 1 | NOTE: Add id of table (2) = id of table (1)
| 22 | 2 |
| 23 | 3 |
| 24 | 4 |
+----+----------+

类型名称 (3)

+----+-----------+--------------+
| id | Type id | Name |
+----+-----------+--------------+
| 31 | 21 | Text add |
| 32 | 22 | Banner | NOTE: Type id of table (3) = id of table (2)
| 33 | 23 | Video add |
| 34 | 24 | Other |
+----+-----------+--------------+

我需要这样的报告:

+--------+-----------+--------------+
| Add id | Add name | Type of add |
+--------+-----------+--------------+
| 1 | Add01 | Text add |
| 2 | Add02 | Banner |
| 3 | Add03 | Video add |
| 4 | Add04 | Other |
+--------+-----------+--------------+

(添加 id 来自表 (1),添加名称 来自表 (1),添加类型 来自表 (3))

到目前为止,我可以执行 SELECT query LEFT JOIN table 1 and 2 但我不知道如何从表 3 返回值 Name of type。如何我可以做吗?

最佳答案

再次加入表3:

SELECT t1.`id`          AS `Add id`,
t1.`name of add` AS `Add name`,
t3.`Name` AS `Type of add`
FROM `ADDS` AS t1
LEFT JOIN `TYPE OF ADDS` AS t2 ON t2.`Add id` = t1.`id`
LEFT JOIN `NAMES OF TYPES` AS t3 ON t3.`Type id` = t2.`id`

关于mysql - 基于第二个表 ID 从第三个表字段返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10838386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com