Md. Sajedul Karim
Md. Sajedul Karim

Reputation: 7065

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 post_id 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.

Upvotes: 0

Views: 48

Answers (1)

Arulkumar
Arulkumar

Reputation: 13237

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

SELECT P.*
FROM Post P
JOIN fevorite F ON F.post_id = P.post_id
WHERE F.fevorite_by = 2

Upvotes: 1

Related Questions