选择列等于另一个查询的任何值的位置

时间:2014-10-21 04:55:20

标签: php mysql

我有一个查询来查找名为entryEntryPairs的表中的关系:

if (!$result = $mysqli->query(
    "SELECT entryIdA AS relatedId FROM entryEntryPairs WHERE entryIdB = ".$inRow["id"].
    " UNION ".
    "SELECT entryIdB AS relatedId FROM entryEntryPairs WHERE entryIdA = ".$inRow["id"]
)) return "Something went wrong";

但是,我真正想要的是将所有relatedId用作对名为entry的表的更大查询的一部分。形式的东西:

SELECT title FROM entry WHERE id =

id =右侧缺失的部分将是relatedIds的全部内容。这可以在一个较大的查询中完成,还是必须迭代relatedId右侧每个id =插入的第一个查询的结果?

1 个答案:

答案 0 :(得分:3)

您可以尝试使用IN

"SELECT title FROM entry WHERE id IN (SELECT entryIdA AS relatedId FROM entryEntryPairs WHERE entryIdB = ".$inRow["id"].
" UNION ".
"SELECT entryIdB AS relatedId FROM entryEntryPairs WHERE entryIdA = ".$inRow["id"].")"