如何添加第二个查询作为条件?

时间:2012-05-15 00:09:11

标签: php

请参阅下面的查询。现在它从联系人那里查找system_id,其中system_id =' $ sid'但我希望查询还要从联系人中查找friend_id,其中system_id =' $ sid'

$tre = mysql_query("SELECT System_id, Full_name FROM accounts 
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");
while($re=mysql_fetch_array($tre))
{
   $qry=mysql_query("SELECT System_id FROM contacts WHERE 
   System_id='". $sid ."' AND Friend_id='". $re['System_id'] ."'");
   if(mysql_num_rows($qry)>0){
      if($sid!=$re['user_id']){
         $obx['id']=$re['System_id'];
         $obx['name']=$re['Full_name'];
         $arr[]=$obx;
      }
   }

我如何调整它?

谢谢!

更新:

在查看下面的一些评论后,我构建了这个按预期工作的查询:

SELECT DISTINCT contacts.friend_id, accounts.full_name, 
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id 
= accounts.system_id) OR (contacts.friend_id = '$sid' 
AND contacts.system_id = accounts.system_id)

唯一的问题是我如何更改SELECT查询:

$tre = mysql_query("SELECT System_id, Full_name FROM accounts 
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");

To $ tre:

SELECT DISTINCT contacts.friend_id, accounts.full_name, 
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id 
= accounts.system_id) OR (contacts.friend_id = '$sid' 
AND contacts.system_id = accounts.system_id)

0 个答案:

没有答案