Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用不同id作为条件选择具有相同id的行?_Mysql_Subquery - Fatal编程技术网

Mysql 使用不同id作为条件选择具有相同id的行?

Mysql 使用不同id作为条件选择具有相同id的行?,mysql,subquery,Mysql,Subquery,我有下表: +----+-------------+-----------------+-----------+----------------+ | id | link_id[FK] | category_id[FK] | parent_id[FK] | sort_order | +----+-------------+-----------------+-----------+----------------+ | 1 2 1

我有下表:

+----+-------------+-----------------+-----------+----------------+
| id | link_id[FK] | category_id[FK] | parent_id[FK] | sort_order |
+----+-------------+-----------------+-----------+----------------+
| 1       2             1                 1               1       |
| 2       2             133               1               2       |
| 3       3             2                 2               1       |
| 4       3             200               2               2       |
| 5       3             333               200             3       |
| 6       4             1                 1               1       |
| 7       5             3                 3               1       |
| 8       5             133               3               2       |
| 9       5             223               133             3       |
| 10      5             456               223             4       |
+-----------------------------------------------------------------+
我需要能够
选择
具有相同
链接id的所有行,但使用
类别id
作为条件。下面是我要寻找的示例结果

+----+-------------+-----------------+-----------+----------------+
| id | link_id[FK] | category_id[FK] | parent_id[FK] | sort_order |
+----+-------------+-----------------+-----------+----------------+
| 1       2             1                 1               1       |
| 2       2             133               1               2       |
| 7       5             3                 3               1       |
| 8       5             133               3               2       |
| 9       5             223               133             3       |
| 10      5             456               223             4       |
+-----------------------------------------------------------------+
我尝试过这个查询,但它只返回等于
category\u id
133的行

SELECT *
FROM table a
WHERE (a.object_id,a.category_id) IN (
    SELECT b.link_id,b.link_id
    FROM table b
    WHERE b.category_id = 133
    AND a.link_id = b.link_id

) ORDER BY a.sort_order 

我还尝试了自联接,这基本上就是我所需要的,但是我得到了单独的列,我需要像上面那样将它们组合起来。

首先需要从
category\u id=133
的表中获取
链接id
,然后查询表中所有具有该
链接id的行

SELECT * FROM `table`
WHERE link_id IN (
    SELECT link_id FROM `table`
    WHERE category_id = 133
)
ORDER BY link_id, sort_order
演示:

您还应该能够通过“自连接”来实现这一点:


演示:

谢谢火箭,它工作得很好。您能告诉我如何指定两个
类别id
?比如说1和133?没关系,我发现我可以用第二个
category\u id
对第一个查询执行第二个
WHERE-category\u id条件。
WHERE-category\u id IN(1133)
?如果这样做很好,但它会得到所有具有一个或另一个id的行。
SELECT a.* FROM `table` a
JOIN `table` b ON a.link_id = b.link_id
WHERE b.category_id = 133
ORDER BY a.link_id, a.sort_order