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
https://forums.phpfreaks.com/topic/215652-mysql-subquery/
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
https://forums.phpfreaks.com/topic/215652-mysql-subquery/#findComment-1121291
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.