0

Anyone could tell me how to connect these 2 tables. I tried do it myself.. but just wasted a time, i know it's easy but I somehow can't understand it. Table is from my previous question

Table Articles:

    ID     Content   
    1          bla 
    2          blah 
    3          etc.
    4          whatever

Table Similar:

    ID     Similar_ID   
    3          1
    3          2
    4          1
    4          2
    4          3
Community
  • 1
  • 1
Jonuux
  • 533
  • 1
  • 7
  • 20

2 Answers2

0
select a.ID,a.Content,s.Similar_ID from 
Articles a inner join Similar s
on a.ID=s.ID
AnandPhadke
  • 13,160
  • 5
  • 26
  • 33
0

You want to browse the Similar table, and "convert" its IDs (e.g. 3) in Content (e.g. "Blah").

So: SELECT * FROM Similar;

will list all similarities. Since we have two Ids to convert (something is similar to something else), we need two separate JOINS with the same table Articles, and we'll alias them to "a" and "b":

 SELECT a.Content, b.Content
     FROM Similar
     JOIN    Articles AS a      ON (Similar.ID = a.ID)
     JOIN    Articles AS b      ON (Similar.Similar_ID = b.ID)
 ;

The first JOIN "decodes" the ID field of Similar, the second decodes "Similar_ID".

So

3     1

becomes now

Etc.  Blah

Or you can write:

SELECT CONCAT(a.Content, ' is similar to ', b.Content)
FROM... (same query as above)

and get

Etc. is similar to Blah
Blah is similar to whatever
...
LSerni
  • 55,617
  • 10
  • 65
  • 107