开发者

Cross table query

I'm running a query with retrieves user posts. The table Posts has a co开发者_如何转开发lumn with the name of the User who submitted the post, also another table called User has a list of usernames and avatars.

I need to look up in each post for 'User' on table Users and if there is a match echo column "Avatar" from this table.

$result = mysql_query("SELECT * FROM Posts WHERE MATCH (City) AGAINST ('$city2') ORDER by `Comments` DESC LIMIT $limit_posts OFFSET $first_post");

while($row = mysql_fetch_array( $result )) { ?>
<div class='item'>
    <a href="?city=<?php echo $row['City']; ?>&post=<?php echo $row['PID']; ?>"><?php echo $row['Text']; ?></a> 
<? } 


$result = mysql_query("
SELECT
  Posts.*,
  Users.Avatar
FROM
  Posts
INNER JOIN
  Users
ON
  Users.ID = Posts.User
WHERE
  MATCH (Posts.City) AGAINST ('$city2') 
ORDER BY
  Posts.`Comments` DESC 
LIMIT 
  $limit_posts 
OFFSET 
  $first_post
");

while($row = mysql_fetch_array( $result )) { ?>
<div class='item'>
    <a href="?city=<?php echo $row['City']; ?>&post=<?php echo $row['PID']; ?>"><?php echo $row['Text']; ?></a> 

<?php echo $row['Avatar']; ?>
<? } 


This is just a basic table join right?

SELECT USERS.AVATAR, POSTS.TEXT
FROM USERS INNER JOIN POSTS ON USERS.ID = POSTS.USER

Will return a list of user avatar and post text for every user

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜