I want to display data from a foreign key table in my main table, how do I do?

Asked

Viewed 218 times

0

In PHP as I do instead of the foreign key id, the field that has the data appears?

I have a table room that has, column id, spine nome and column tipo.

And also I have the table of movies, the movie rooms I registered in another table and I want to call the type of room, but when I put to display in php, instead of the tipo is appearing the id living room.

Below the code of the tables...

CREATE TABLE IF NOT EXISTS `filmes` (

`id` int(5) NOT NULL AUTO_INCREMENT,

`banner` mediumblob NOT NULL,

`nome` varchar(50) NOT NULL,

`genero` varchar(50) NOT NULL,

`tempo` varchar(50) NOT NULL,

`classificacao` varchar(50) NOT NULL,

`sala_id` int(5) NOT NULL,

`sinopse` varchar(1090) NOT NULL,

PRIMARY KEY (`id`),

KEY  `sala_id_fk` (`sala_id`)

) 

CREATE TABLE IF NOT EXISTS `sala` (

`id` int(5) NOT NULL AUTO_INCREMENT,

`nome` varchar(20) NOT NULL,

`tipo` varchar(20) NOT NULL,

 PRIMARY KEY (`id`)

)

1 answer

1

Dude, basically like this, it’s just relationship between tables (Joins):

SELECT FILMES.ID  
     , FILMES.NOME  
     , SALA.NOME  
     , SALA.TIPO   
  FROM FILMES  
  LEFT JOIN SALA ON (FILMES.SALA_ID = SALA.ID)

Concatenating only the column value Type:

SELECT GROUP_CONCAT(SALA.TIPO) AS TIPO   
  FROM FILMES  
  LEFT JOIN SALA ON (FILMES.SALA_ID = SALA.ID)

That way, you’ll only have the field Type without the need for a While.

  • In this case as I would call only the data that is in the table TYPE field of the room table using mysqli, without having q use the while for this?

  • I edited the answer, see if it meets your need. It is still not very clear to me what you need... :/

Browser other questions tagged

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