$sql = \"select body, stamp from posts where user_id = \'$userid\' order by stamp desc\";
NOTE: the above query works fine. What I want to do is also s
You'll need to use a JOIN to bring the two tables together on the matching field, so something like:
$sql = "SELECT p.body, p.stamp, u.username FROM posts p INNER JOIN users u ON p.user_id=u.user_id WHERE p.user_id='$userid' ORDER BY p.stamp DESC";
$sql = "
select body, stamp from posts where user_id = '$userid' order by stamp desc
UNION ALL
select body, stamp from username where user_id = '$userid' order by stamp desc
";
http://dev.mysql.com/doc/refman/5.0/en/union.html
Try this:
select body, stamp, username
from posts p JOIN users a ON p.user_id = a.user_id
WHERE p.user_id = '$userid' order by stamp desc
select body, stamp, username
from posts,users
where users.user_id = post.user_id
and users.user_id = '$userid'
order by stamp desc;
You should use aliases or table name to avoid the duplicate problem like this
tablename.column
or
alias.column
you can set the alias in your where clause :
FROM table as alias_name
This should work fine:
SELECT posts.body, posts.stamp, users.username
FROM posts, users
WHERE posts.user_id = '$userid' AND posts.user_id = users.user_id
ORDER BY posts.stamp DESC