简体   繁体   English

如何从两个表中获取帖子的用户信息?

[英]How can I pull user info for posts from two tables?

I have two tables, a "users" and "updates" table. 我有两个表,一个“用户”和“更新”表。 I want to grab these values from the tables: 我想从表中获取这些值:

id, username - from the "users" table message, createdOn - from the "updates" table ID,用户名-来自“用户”表消息,createdOn-来自“更新”表

I need to grab these values for a specified user and friends of the user. 我需要为指定的用户和该用户的朋友获取这些值。 I have already created an array containing all friends id's that would look like this: 我已经创建了一个包含所有好友ID的数组,如下所示:

array(1,3,7,21,45);

I need a query that will select all of the updates with an matching the previous array. 我需要一个查询,该查询将选择与先前数组匹配的所有更新。 The "updates" table also has a column "uid" which defines the user who posted the update. “更新”表还具有一列“ uid”,该列定义了发布更新的用户。 This is what I have so far: 这是我到目前为止的内容:

$updates = "SELECT users.id, users.username, updates.message, updates.createdOn FROM users, updates WHERE updates.uid = '". $_COOKIE['id'] ."' ". $friendsNews ." ORDER BY updates.createdOn DESC LIMIT 0, 10";
$updates = mysql_query( $updates );

This isn't working for me though, I created a foreach loop that also creates an or statement which is inserted into the query "$friendsNews" that looks like this: 但是,这对我不起作用,我创建了一个foreach循环,该循环还创建了一个or语句,该语句插入查询“ $ friendsNews”中,如下所示:

foreach( $myFriends as $fid ) {
     $friendsNews .= " OR updates.uid = '". $fid ."'";
}

Once the entire query compiles the result of the query is this: 整个查询编译后,查询结果如下:

SELECT users.id, users.username, updates.message, updates.createdOn FROM users, updates WHERE updates.uid = '1' OR updates.uid = '2' OR updates.uid = '3' ORDER BY updates.createdOn DESC LIMIT 0, 10

If someone could help I would really appreciate it, I'm not very experienced with SQL join statements. 如果有人可以帮助我,我将非常感激,我对SQL连接语句不是很有经验。 Thanks! 谢谢!

Try 尝试

$sql = "SELECT users.id, users.username, updates.message, updates.createdOn, ". 
" updates.message, updates.createdOn ".
" FROM users ".
" INNER JOIN updates ON updates.uid = users.id ".
" WHERE  ".
" updates.uid  IN ( ".implode(', ', $myFriends)." )";

I think that if you want to grab all updates for the users specified in the ids array, the simplest way would be to use a sql JOIN and an IN clause 我认为,如果要获取ids数组中指定的用户的所有更新,最简单的方法是使用sql JOININ子句

select users.id, users.username, u1.message, u1.createdOn
from updates as u1
join users as u2 on u2.id = u1.uid
where u1.uid in(1,3,7,21,45)
order by u1.createdOn desc limit 0, 10;

Try this 尝试这个

if (in_array($_COOKIE['id'], $myFriends)) {
   $flist = implode(',', $myFriends) 
}else{
   $flist =  $_COOKIE['id'].','.implode(',', $myFriends);   //if $_COOKIE['id'] is differed.....
}

$sql = "SELECT users.id, users.username, updates.message, updates.createdOn,          
          updates.message, updates.createdOn FROM users INNER JOIN updates
              updates.uid = users.id WHERE updates.uid  IN ( ".$flist." )";

Help for Logged user is differed..... 登录用户的帮助有所不同.....

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

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