Two fields of a table associated with One field of another table

Asked

Viewed 140 times

1

Hello, I’m developing a system that in it I have a table called 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 didn’t succeed is this. take the id_time_1 and id_time_2, be associated in only the table id teams, tried the INNER JOIN and did not succeed.

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

    what is the purpose of id_time_1 and id_time_2? This looks like you need a new table ...

  • Ex: the team table has two record. id = 1, name = Barcelona and photo = upada normally. and the other record. id = 2, name = Real Madrid and photo = upada normally. And the posts table has a record. id = 1, title = Post test, id_time_1 = 1, id_time_2 = 2. how would I have q fzr this query

  • I think I got it, I could put the select you tried on the question?

  • here the select I tried. SELECT * FROM equipes INNER JOIN posts ON equipes.id = posts.id_time_1 AND equipes.id = posts.id_time_2

  • Would you just do a query not be better? Once you have the post data, just do a select for each id_time. I find it cleaner for maintenance than the joins down here.

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 names of the teams related to it. Then you need the main select to use the table post and use your own Join with equipe for each team.

  • Thank you very much, solved my problem mt!

0

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

You said the records have the following values:

Table teams:

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

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

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

Realize that there is a logic error, because the value of id_time_1 = 1 and id_time_2 = 2. Then when compared to the id 1 of the teams table the first condition will be true and the second false (resulting in false and will not bring anything). When compared to the id 2 of the teams table the first condition will be false (then neither fall in the second and bring nothing too).

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

SELECT * FROM equipes e
INNER JOIN posts p ON p.id_equipes = e.id
  • Alan, had already done so and also, had not worked out, but still thank you for the willingness to understand my problem :D

  • To do this you should create the id_teams field in the posts table with the respective id of the teams table for each posts. This would work. But anyway, the most important thing is to solve the problem and n necessarily as! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.