gpt4 book ai didi

MySQL - 显示记录及其父项

转载 作者:行者123 更新时间:2023-11-30 22:45:03 24 4
gpt4 key购买 nike

<分区>

如何显示特定记录及其父项(最多两个)?

Table:
+---------------+
| id | parentID |
+---------------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
+---------------+

The expected result:
Showing record with id 4 and his two parents

+---------------+
| id | parentID |
+---------------+
| 1 | null |
| 2 | 1 |
| 4 | 2 |
+---------------+

我不知道怎么办。以下查询无效。

(SELECT `c1`.`parentID` FROM `table` as `c1` WHERE `c1`.`id` = 4 LIMIT 1) AS `c1parentID`,
(SELECT `c2`.`parentID` FROM `table` as `c2` WHERE `c2`.`id` = `c1parentID` LIMIT 1) AS `c2parentID`,
SELECT * FROM `table` as `c3` WHERE `c3`.`id` = `c1parentID` OR `c3`.`id` = `c2parentID` OR `c3`.`id` = 4

我对高级查询的了解很差。请帮助:)

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