0

我有一个包含两列的表。这两列是 object_id 和 term_taxonomy_id。假设表格如下所示:

object_id | term_taxonomy_id
----------------------------
111       | 75
119       | 42
234       | 34
742       | 75
824       | 99
931       | 34
999       | 75

现在假设我知道object_id的值,并且我想检索所有具有相同 term_taxonomy_id 的对象 ID

例如,假设我有对象 ID 742,我想获取term_taxonomy_id 为 75的所有 object_id 记录,因此我应该有类似

111、742、999

是否可以通过单个 SQL 查询来实现?

我使用的 DBMS 是 MySQL。

亲切的问候 :)

4

2 回答 2

1

采用GROUP_CONCAT

SELECT GROUP_CONCAT(Object_ID) ObjectList
FROM tableName
WHERE term_taxonomy_id = 75

更新 1

SELECT GROUP_CONCAT(object_id) ObjectList
FROM   tableName
WHERE  term_taxonomy_id = 
        (
          SELECT term_taxonomy_id
          FROM   tableName
          WHERE  object_id = 742
        )

或通过使用JOIN

SELECT GROUP_CONCAT(a.object_id) ObjectList
FROM   tableName a
       INNER JOIN tableName b
       ON a.term_taxonomy_id = b.term_taxonomy_id AND
               b.object_id = 742
于 2013-02-08T09:05:15.743 回答
1

使用下面的查询:

SELECT DISTINCT t2.object_id FROM table as t1, table as t2
WHERE t1.object_id = XX
AND t2.term_taxonomy_id = t1.term_taxonomy_id
于 2013-02-08T09:11:51.560 回答