Md. Sajedul Karim Md. Sajedul Karim - 1 year ago 93
MySQL Question

Get data from joined table in mysql

Please check the image.

two table, fevorite table and post table.

Here i have 2 table. One is post table and another is fevorite table. Both has primary key and

is FOREIGN_KEY in fevorite table.

Now my needs are:

  1. Select post_id list from fevorite table WHERE fevorite_by=2.

  2. Using this post_id list get post details (post_title,created_by, ...) from post table.

I need all of those things in single query. Thanks in advance.

Answer Source

Use sub query to get your result:

SELECT * FROM Post WHERE post_id IN (
SELECT post_id FROM fevorite WHERE fevorite_by = 2)

Or you can do it by JOIN

JOIN fevorite F ON F.post_id = P.post_id
WHERE F.fevorite_by = 2