-1

I have two simple tables, with the following build form:

TABLE 1

id - name

TABLE 2

id - name - post_id (table1.id Fkey)

What I want is, to get the data from the TABLE 1 where the TABLE 1's id matches TABLE 2's post_id

It's pretty easy, but I have 37 records and I'm getting only 13 records.

Query:

SELECT posts.id FROM posts INNER JOIN favorites ON posts.id = favorites.post_id

When the above query is executed, the result is:

Some records that are shared by the relation (id = post_id), and the rest records are random with post_id value as NULL

Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • 1
    37 records in which table, posts? Only getting 13 records might be correct, for not all posts may be in the favorites where the two id's match. Does this make sense? You'll have to check the id data between the two and see what exists ... unless you're looking for a `LEFT JOIN` instead, perhaps? – Paul T. Jul 19 '17 at 00:17
  • `select T1.* from Table1 T1, Table2 T2 where T1.id = T2.post_id` - try this. I think it returns what you want. – Dalton Cézane Jul 19 '17 at 00:22
  • Table 1, the `posts` table – Jaeger Jul 19 '17 at 00:25
  • I have checked, at least 24-25 records are shared by the same id in both tables, thats why i've put this question here. – Jaeger Jul 19 '17 at 00:26
  • The quesiton's problem is, records are NOT getting displayed as they should be! i know how to join but i don't know where did i mess up here in this query, as it's pretty simple JOIN query. – Jaeger Jul 19 '17 at 00:32
  • I have updated the question. – Jaeger Jul 19 '17 at 00:44

1 Answers1

1

Try it and show result

SELECT posts.id FROM posts LEFT JOIN favorites ON posts.id = favorites.post_id