的functions.php
function getfriendone($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_one`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
function getfriendtwo($id, $field){
$query = mysql_query("SELECT `$field` FROM `friends` WHERE `user_two`='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
的index.php
$fetch_friends = $_SESSION['user_id'];
$two = getfriendone($fetch_friends, 'user_two');
$three = getfriendtwo($fetch_friends, 'user_one');
$result = mysql_query("SELECT * FROM posts WHERE id IN ('$two', '$three')");
echo $two." | ".$three;
表(朋友)的getfriend功能
ID user_one user_two
1 1 2
2 1 3
3 4 1
$ result
的表格(帖子)ID TEXT NAME
1 Hello Bob
1 Hello Bob
2 Hello Mark
我想在这里做的是回显user_one和user_two中包含整数1的所有行。在我的追逐中,只有ID 1和3将在此处回显。它选择了第一场比赛,只回过那场比赛。它可以正常工作,但只能与列中的第一个匹配
答案 0 :(得分:1)
如果我读得正确:
SELECT *
FROM posts s
WHERE
EXISTS
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_one) OR
(SELECT 1 FROM posts s2 WHERE s.id = s2.user_two)
将提取您需要的数据
SQLFiddle - > http://sqlfiddle.com/#!2/9ae070/3/0