【问题标题】:MySQL Select data from table1 based on table2MySQL根据table2从table1中选择数据
【发布时间】:2016-10-10 17:16:36
【问题描述】:

我想SELECT数据FROMstatustableWHEREaccount_name or author$logname$username如果account_name or authorfriends987654334@9334的朋友p>

这里是status table

数据 account_name 作者

1 你好 约翰 约翰

2 我很好 约翰 Doe

3 Doe James

4 谁是谁? 詹姆斯 史密斯

5 约翰 威廉姆斯

6 地狱 银行 詹姆斯

这里是friends table

user1 user2

约翰 Doe

詹姆斯 Doe

史密斯 詹姆斯

威廉姆斯 约翰

银行 詹姆斯

我想做的是能够SELECT 来自status table 的所有数据,其中account_name and author 是John 或Doe 或John 的朋友,即Williams。

所以当$username="Doe"$logname="John"时查询的输出应该是1、2、3和5,但是当$username="Doe"$logname="Doe"时应该是1、2、3、4、5和6。

这是我迄今为止尝试过的,但我从status 或只是result 获得所有结果,其中只有account_name or author$logname$username

$username = "Doe";
$logname = "John"; 

$query=mysqli_query($db_conx, "SELECT s.id, s.account_name, s.author, s.data, s.postdate  FROM status s INNER JOIN  friends f ON f.user1='$username' OR f.user2='$username' WHERE s.account_name='$username' OR s.author='$username' OR s.account_name='$logname' OR s.author='$logname' GROUP BY s.id ORDER BY s.postdate DESC");


    //I have also tried these two queries but not giving me what I want

//$query = mysqli_query($db_conx, "SELECT * FROM status WHERE account_name = '$username' OR author = '$username' ORDER BY postdate DESC");
    $num_row = mysqli_num_rows($query);
    echo " Numbers ".$num_row;
    //$query =mysqli_query($db_conx, "SELECT s.* , f.* FROM status s, friends f WHERE s.account_name='$user' AND f.user1='$user' OR f.user2='$user'");


    while($row=mysqli_fetch_array($query))
    {


?>
    <tr>
    <td><p><?php echo $row['data']; ?></p></td>
    <td><p><?php echo $row['id']; ?></p></td>
    <td><p><?php echo $row['author']; ?></p></td>
    <td><p><?php echo $row['account_name']; ?></p></td>
    </tr>


<?php   
    }

到目前为止,我已经检查过,如果有任何帮助,我们将不胜感激。

Two mysqli queries

Difference between left join and right join in SQL Server

How can an SQL query return data from multiple tables

Select results from table1 based on entries on table2

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    看来你是想SELECT记录好友的好友时发在好友上还是好友发在他的好友上?类似于 facebook 提要。 我不知道是否有更好的方法来做到这一点,但你可以试试下面的代码,需要改进它或者有人可以在这里改进它。

    $query = mysqli_query($db_conx, "SELECT * FROM friends WHERE user1='kira' OR user2 = 'mandekira' OR user2='kira' OR user1 = 'mandekira'");
    $num_row = mysqli_num_rows($query);
    ?>
    <table>
    <?php
    while($row=mysqli_fetch_array($query))
    {
        $sql = mysqli_query($db_conx, "SELECT * FROM status WHERE (account_name='".$row['user1']."' AND author='".$row['user2']."') OR (account_name='".$row['user2']."' AND author='".$row['user2']."') ORDER BY postdate DESC");
        while($row=mysqli_fetch_array($sql))
    {
    ?>
        <tr>
        <td width="100"><?php echo $row['data']; ?></td>
        <td width="100"><?php echo $row['account_name']; ?></td>
        <td width="100"><?php echo $row['author']; ?></td>
        </tr>
    <?php   
    }
        }
    ?>
    </table>
    <?php
    

    【讨论】:

    • 这正是我想要的。但是我可以在一个查询中得到它吗?
    【解决方案2】:

    样本数据真的很有帮助,我想出了这个:

    $query=mysqli_query($db_conx, "
     SELECT
    `status`.id,
    `status`.account_name,
    `status`.author,
    `status`.`data`
     FROM
    `status`
    LEFT JOIN friends AS acc_friends ON `status`.account_name IN (acc_friends.friend1, acc_friends.friend2)
    LEFT JOIN friends AS au_friends ON `status`.author IN (au_friends.friend1, au_friends.friend2)
    WHERE
    '$logname' IN (acc_friends.friend1, au_friends.friend1,acc_friends.friend2, au_friends.friend2)
    OR '$username' IN (acc_friends.friend1, au_friends.friend1,acc_friends.friend2, au_friends.friend2)
     ");
    

    起初我误解了您的需求,这次是从作者或帐户中提取每条朋友的记录,然后通过搜索结果搜索BanksDoe(将它们更改为登录名和用户名)朋友列表,因为再次搜索帐户和作者只是多余的。

    这个查询应该可以解决问题:)

    【讨论】:

    • 谢谢,让我去我的电脑,我会尽力让你知道的。
    • 我得到了与我在问题中发布的查询相同的输出。但是我已经为我的问题添加了表格结构和更多解释。谢谢。
    • 我用一个应该可以解决这个问题的查询更新了答案
    • 很抱歉回复晚了,我一直在尝试重新操作您的代码,看看我是否可以实现它,但没有成功。
    • 是的,您只需要更改这两个...好像它会返回大量记录只是为了向您显示帐户之间的真实关系->朋友和作者->您需要@的朋友987654324@它以缩小结果范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多