繁体   English   中英

MySQL基于表2从表1中选择数据

[英]MySQL Select data from table1 based on table2

我想SELECT的数据FROM status table WHERE account_name or author或者是$logname$username ,如果account_name or author是从朋友friends table

这是status table

数据 account_name 作者

1 你好 约翰 ·约翰

2 我是好 约翰· 多伊

3 Please Doe James

4 谁是? 詹姆斯· 史密斯

5 ·约翰· 威廉姆斯

6 地狱 银行 詹姆斯

这是friends table

USER1 USER2

约翰·

詹姆斯·

史密斯 ·詹姆斯

威廉姆斯 ·约翰

银行 詹姆斯

我想要做的是能够从status tableSELECT所有数据,其中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或仅account_name or author$logname$usernameresult中获取所有结果。

$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   
    }

到目前为止,我已经检查过,请提供任何帮助。

两个mysqli查询

SQL Server中左联接和右联接之间的区别

SQL查询如何从多个表返回数据

根据表2上的条目从表1中选择结果

该示例数据确实很有帮助,我想到了:

$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 (将它们更改为登录名和用户名),因为在帐户和作者再次只是多余的。

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

似乎当您张贴在好友上的朋友或发布在他的好友上的朋友时,您正在尝试SELECT好友的记录? 类似于facebook feed。 我不知道是否有更好的方法可以执行此操作,但是您可以尝试下面的代码,需要对其进行改进,或者有人可以在此处进行改进。

$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

暂无
暂无

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

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