0
$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 select username from my users table, and display the username that matches the user_id.

I have edited the above statement like so, and it doesnt work. Can someone suggest the correct query? My goal is to also be able to display usernames. in addition to simply displaying user_id.

$sql = "select body, stamp from posts AND username from users where user_id = '$userid' order by stamp desc";

My goal is to also be able to display usernames. instead of simply user_id.

2

9 Answers 9

4

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";
Sign up to request clarification or add additional context in comments.

Comments

1

You can use table name to select the columns. Ex:

$Query = "select table1.body, table1.stamp, users.username from posts, users where user_id = '$userid' order by stamp desc";

But, this method is not good in performance.

The best method is:

$Query = "SELECT table1.body, table1.stamp, users.username 
FROM posts 
INNER/LEFT/RIGHT JOIN users 
ON users.user_id = '$userid' AND users.user_stamp = stamp.stamp_id

All the tables must be related.

Greetings,

Comments

0
$sql = "select body, stamp from posts , username from users where user_id = '$userid' order by stamp desc";

I just corrected the syntactical error in your query... that is the AND keyword should be replaced by ,.

1 Comment

This will result in a cartesian product, many more rows than expected. You need a join condition between the two tables, and the implicit join syntax (comma-separated tables) is old and scheduled for deprecation.
0
$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

Comments

0

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

Comments

0
$sql = "SELECT p.body,p.stamp,u.username from posts p LEFT JOIN users as u ON (p.user_id = u.user_id) WHERE user_id = '$user_id' ORDER BY p.stamp DESC";

should do it

Comments

0

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

Comments

0

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

Comments

0
 select body, stamp, username 
    from posts,users 
    where users.user_id = post.user_id 
        and users.user_id = '$userid' 
    order by stamp desc;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.