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。它选择第一个匹配项,并且仅回显该匹配项。它可以正常工作,但仅与列中的第一个匹配项匹配

最佳答案

如果我阅读正确:

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

关于php - 不只选择列中的第一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27565130/

10-16 18:58