donglin5770 2014-10-21 04:55
浏览 14
已采纳

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

I have a query to find relationships in a table named 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 relatedIds as part of a larger query to a table named 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. 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 =?

  • 写回答

1条回答 默认 最新

  • duanliangman5398 2014-10-21 05:10
    关注

    You can try with IN like

    "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"].")"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题