Jump to content

Mysql subquery


dlouis95

Recommended Posts

Hello all. Im making a new site and I've been doing good up until this one part that i never learned how to do. What i need to do is select posts (posts table) where the person that made the post is friends with me (friend table) and I would like it in order of it being made. So far the closest ive gotten in getting this is the code below

 

SELECT * FROM `posts` WHERE `user_id`=(SELECT `friend_id` FROM `friend` WHERE user_id = ' #my id ')

 

But this gave the error that I'm selecting more than one row, which I need to do. Is there any other way I can do this? Any help at all would be greatly appreciated. I'm using Mysql client version 5.1.50. :)

Link to comment
Share on other sites

If you expect to have more than one friend, you can use IN instead of = :

SELECT * FROM `posts` 
WHERE `user_id` IN (SELECT `friend_id` FROM `friend` WHERE user_id = ' #my id ')

 

:D :D :D :D :D :D :D :D :D :D :D :D :D

THANK YOU SOO SOOOO SOOOOOOOO MUCH. lol I wish my friends knewmysql more. Thanks you. =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.