简体   繁体   English

不只选择列中的第一行

[英]Not select only first row in column

functions.php 的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 的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;

Table (friends) for getfriend function 表(朋友)进行getfriend功能

ID    user_one    user_two
1     1           2
2     1           3
3     4           1

Table (posts) for $result $ result的表(帖子)

ID    TEXT    NAME
1     Hello   Bob
1     Hello   Bob
2     Hello   Mark

What i wanna do here is echo all the rows that has integer 1 in it in both user_one and user_two. 我在这里想要做的是在user_one和user_two中回显所有具有整数1的行。 In my chase, only ID 1 and 3 will be echoed here. 按照我的追踪,此处仅回显ID 1和3。 It picks the first match and echos only that one. 它选择第一个匹配项,并且仅回显该匹配项。 It works as it should, but only with the first match in the column 它可以正常工作,但仅与列中的第一个匹配项匹配

If i read properly: 如果我阅读正确:

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)

Will extract the data you need 将提取您需要的数据

SQLFiddle -> http://sqlfiddle.com/#!2/9ae070/3/0 SQLFiddle-> http://sqlfiddle.com/#!2/9ae070/3/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM