Friday, February 24, 2012

A question about SQL queries

Ok, I'm sorry if this has already been answered. I didn't know what to look for (or I would've solved my problem on my ownSmile)

So I have two tables, one called"users" and the other one called"posts". I'm retrieving everything from the posts-table and the userid and username from the users-table. The thing I can't get working is that I ONLY want toretrieve the username for thespecific userid that made the post. I'm sorry for the lack of explanation and the lack of code (since I actually don't know what to do I don't have any code) and the fact that english isn't my native language.

Thanks in advance, if there's anyone that understands meStick out tongue


standard SQL statement for this problem would be:

assume table user has following columns : userid,username table post has following column : userid, postreply

select a.userid,a.username,b.postreply

from post b,user a

where b.userid=a.userid

now you have to adapt the statement for you flavor of database.

if you use the sql query editor to attach to your database type, it should generate the correct style for your command

to ensure that you get all records from one table even if there is no matching record in the other table then you start playing with inner joins and outter joins.

|||

Fair enough :D I will try that next week, going away for the weekend but I will get back to you if this worked out for me :)

EDIT: Just wanted to say I worked out fine for me :)

"SELECT users.userid, users.username, posts.postid, posts.userid, posts.message FROM users, posts WHERE users.userid = posts.userid"

No comments:

Post a Comment