请在下面查看我的查询。现在它从 system_id = '$sid' 的联系人中查找 system_id 但我希望查询也从 system_id = '$sid' 的联系人中查找friend_id
$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");
到$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)