left join查找匹配ID

时间:2015-05-27 14:43:02

标签: php sql join

我有2个表,我想要离开加入,但我不知道该怎么做才能从两个表ON ".$FT.".userid = ".$MT.".to_id中找到一个ID,因为在messageThread中,SESSION id可以是to_id或者from_id取决于谁开始对话(msg_thread)...有没有办法在查询(SQL)中执行此操作,或者我是否必须在此上面添加另一个查询并找到与SESSION匹配的ID?

  1. 表是朋友
  2. 表格是messagethread
  3. 这是代码:

    $FT = $table['friends'];    
    $MT = $table['messagethread'];
    
    $query = "SELECT userid, status, fuserid, to_id, from_id
              FROM ".$FT." 
              LEFT JOIN ".$MT."
              ON ".$FT.".userid = ".$MT.".to_id
              WHERE userid = ? && status = ? && (to_id = ? OR from_id = ?)";
    $params = array($_SESSION['USER_ID'], '2', $_SESSION['USER_ID'], $_SESSION['USER_ID']);
    $results = dataQuery($query, $params);
    

1 个答案:

答案 0 :(得分:0)

尝试将where条件设为类似

WHERE .$_SESSION['USER_ID']. IN (userid, to_id, from_id)