Sql 使用两张桌子。我做错了什么?

Sql 使用两张桌子。我做错了什么?,sql,join,union,Sql,Join,Union,我想创建一个活动订阅源系统,我的订阅源状态和好友位于数据库的不同表中。我如何连接他们,以便登录用户只能从他们的朋友那里接收提要 <?php $sql = " SELECT * FROM status WHERE author='(friend of logged-in user)' AND type='a' **UNION** SELECT * FROM friends WHERE user1='$user' AND accepted='1' OR user2='$user' AND a

我想创建一个活动订阅源系统,我的订阅源状态和好友位于数据库的不同表中。我如何连接他们,以便登录用户只能从他们的朋友那里接收提要

<?php
$sql = "
SELECT * FROM status WHERE author='(friend of logged-in user)' AND type='a'
**UNION** 
SELECT * FROM friends WHERE user1='$user' AND accepted='1' OR user2='$user' AND accepted='1' 
";
$query = mysqli_query($database, $sql);
$statusnumrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $user1 = $row["user1"];
    $user2 = $row["user2"];
    $accepted = $row["accepted"];
    $statusid = $row["id"];
    $account_name = $row["account_name"];
    $author = $row["author"];
    $postdate = $row["postdate"];
    $postdate = strftime("%b %d, %Y %I:%M %p");
    $data = $row["data"];
    $data = nl2br($data);
    $data = str_replace("&amp;","&",$data);
    $data = stripslashes($data);
    $statusDeleteButton = '';
    if($author == $log_username || $account_name == $log_username ){
        $statusDeleteButton = '<span id="sdb_'.$statusid.'"><a href="#" onclick="return false;" onmousedown="deleteStatus(\''.$statusid.'\',\'status_'.$statusid.'\');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a></span> &nbsp; &nbsp;';
    }

    $feedlist .= '<div id="status_'.$statusid.'" class="flipwrapper pin">
        <div class="picture">
        <header class="img-btm">
            '.$postdate.'</b><br />
            20 <a href="#">cmts</a>&nbsp;255 <a href="#">likes</a>&nbsp; '.$statusDeleteButton.'
        </header>
        <a href="status_frame.php?id='.$statusid.'"><img id="bound" src="'.$data.'"/></a></div></div>';
}
?>

您肯定不希望在这里使用联合,而是一个子查询,大致如下所示:

SELECT * FROM status 
WHERE author in (
        SELECT whateverfieldshouldmapfromfriendstoauthor FROM friends 
        WHERE user1='$user' AND accepted='1' OR user2='$user' AND  accepted='1' 
    ) AND type='a'
还有一些一般提示:

在本例中,直接用脚本语言开发查询php是一个非常糟糕的主意。使用允许您开发查询、运行查询和检查结果集的工具。有很多,比如MySQL自己的和 在SQL注入问题上要非常小心。如果$user变量是由请求提供的,那么您就是。避免SQL注入问题的最佳方法是使用。
首先,谢谢大家!我正在测试这一点。但是,我得到了一个错误:警告:mysqli_num_rows期望参数1是mysqli_结果,在第32行的C:…\home.php中给出的布尔值警告:mysqli_fetch_数组期望参数1是mysqli_结果,在第33行的C:…\home.php中给出的布尔值是:$statusnumrows=mysqli_num_rows$query;当$row=mysqli_fetch_array$query时,mysqli_ASSOC{我不知道在SELECT[HERE]中的WHERE author中插入什么FROM friends再次感谢!friends表中的字段包含列作者状态下可以获取的值。可能首先分别从friends中的SELECT*运行子查询,其中user1='$user'和accepted='1'或user2='$user'和accepted='1',应该很明显哪个列包含要查找统计的值我们的作者栏。好的,顺便说一句,非常感谢。最后一件我想不出来的事情是,我合并了两个字段user1和user2,因为它们代表好友请求发送者和接收者,所以登录用户可以是其中之一。所以代码是:SELECT*FROM status,其中author在SELECT user1+user2中作为user3 FROM friends在user1='$log\u username'和accepted='1'或user2='$log\u username'和accepted='1'和type='a'的情况下,CONCAT对我不起作用。顺便说一句,它一直在向我提供来自所有用户的提要。我在那里做错了什么?@sienna在不知道实现细节的情况下很难说,但如果它起作用,它可能是正确的:-哦,我错了很抱歉,我发布得太快了,所以它不完整。问题现在已经解决了。我们不知道您的表是什么样子的。您应该进一步指出问题所在,它似乎与SQL有关,而不是与PHP有关。