Two fields from one table associated with One field from another table

1

Hello, I'm developing a system that has a table named teams , and another table is called posts .

Tabela equipes
id, nome, foto

Tabela posts
id, titulo, id_time_1, id_time_2

What I really wanted and tried to do and did not succeed at is the following. get the id_time_1 and id_time_2, be associated in just the id of the teams table, I tried the INNER JOIN and I was not successful.

SELECT * FROM 'equipes'
INNER JOIN 'posts' ON 'equipes'.'id' = 'posts'.'id_time_1'
AND 'equipes'.'id' = 'posts'.'id_time_2'
    
asked by anonymous 11.05.2017 / 18:34

2 answers

1

From what I understand, what you want is:

SELECT * FROM 'posts'
JOIN 'equipes' AS 'e1' ON 'posts'.'id_time_1' = 'e1'.'id'
JOIN 'equipes' AS 'e2' ON 'posts'.'id_time_2' = 'e2'.'id'

Your data recovery strategy is reversed. You want the post and associate the teams name related to it. Then the main select must use the post table and use a proper join with equipe for each time.

    
11.05.2017 / 19:57
0

You are using INNER JOIN so the select will only return the results if the conditions presented are true.

You said the records have the following values:

Teams table:

id = 1, name = Barcelona and photo = normally uploaded

id = 2, name = Real Madrid and photo = normally upgraded

Table posts:

id_time_1 = 1

id_time_2 = 2

Your select:

SELECT * FROM equipes INNER JOIN posts ON equipes . id = posts . id_time_1 AND equipes . id = posts . id_time_2

Notice that there is a logic error because the value of id_time_1 = 1 and id_time_2 = 2 . So when compared to id 1 of the teams table the first condition will be true and the second false (resulting in false and will bring nothing). When compared to id 2 of the teams table the first condition will be false (so neither would it fall on the second and bring nothing as well).

There are several ways to fix this, but for simplicity and ease I would add a field id_equipes in the post table and would associate that field with select. Example:

SELECT * FROM equipes e
INNER JOIN posts p ON p.id_equipes = e.id
    
11.05.2017 / 19:59