Skyey Skyey - 4 months ago 10
MySQL Question

Database fetch all where exist something from other database table

I have 2 tables

First have id, fromid, post
Second have id, myid, userid


First is for all posts from all users

Second is for favorite users where myid shows id from user that made others as favorites and userid are id from favorited users

Now I want to output only posts from users that are in favorite table with myid

In words I would say output all posts where from id is equal to userid from second table where userid is connected to myid

I tried this but I am getting nothing.

$sqli = "SELECT * FROM posts WHERE
EXISTS (SELECT * FROM favppl WHERE myid='$username' AND userid!='$username')";


OR Question like this:
I want to select all from post table and check does fromid from post table is somewhere in favppl table in userid field connected with myid which is equal to something

Answer

You need to use the id which is going to find the pair in the other table. I wrote the correct query here. You can use your desired id field names. I supposed the column 'myid' keeps the post ids

SELECT * FROM posts WHERE     
id IN (SELECT myid FROM favppl WHERE myid='$postId' AND userid!='$username')