[英]Select where a column equals any value from another query
I have a query to find relationships in a table named entryEntryPairs: 我有一个查询来查找名为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";
However, what I really want is to use all of the relatedId
s as part of a larger query to a table named entry. 但是,我真正想要的是使用所有relatedId
作为对名为entry的表的更大查询的一部分。 Something of the form: 形式的东西:
SELECT title FROM entry WHERE id =
The missing section to the right of id =
would be all of the relatedIds
. id =
右侧的缺失部分将是所有relatedIds
。 Can this be done in a single larger query or do I have to iterate over the results of the first query plugging in each relatedId
to the right of id =
? 这可以在一个更大的查询中完成,还是我必须迭代插入id =
?右边的每个relatedId
的第一个查询的结果?
You can try with IN
like 您可以尝试使用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"].")"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.