links between tables

Asked

Viewed 89 times

0

$sql = "SELECT * FROM Inscricao WHERE al_id = ".$_GET['idc'];
$sql = "SELECT * FROM Aluno WHERE al_id = ".$_GET['idc'];
$sql = "SELECT * FROM EncarregadoDeEducacao WHERE ee_id = ee_id.inscricao where al_id = ".$_GET['idc'];

tables :

CREATE TABLE IF NOT EXISTS `Inscricao` (
 `insc_id`                      int(4) unsigned Not Null AUTO_INCREMENT,
 `al_id`                        int(4) Not Null DEFAULT '0',
 `ee_id`                        int(4) Not Null DEFAULT '0',
 `obs_desc`                     varchar(250)    Not Null DEFAULT 'Sem observacoes',
 `prof_nome`                    varchar(50) Not Null DEFAULT '',
 `insc_data`                    DATE Not Null DEFAULT '00-00-0000', 
  PRIMARY KEY (`insc_id`)
) ENGINE=myisam DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `Aluno` (
 `al_id`                        int(4) unsigned Not Null AUTO_INCREMENT,
 `al_nome`                      varchar(50) Not Null DEFAULT '',
 `al_idade`                     int(2)  Not Null DEFAULT '0',   
 `al_data`                      DATE Not Null DEFAULT '00-00-0000', 
 `al_morada`                    varchar(80) Not Null DEFAULT '',    
 `al_escolaridade`              int(2)  Not Null DEFAULT '0',   
 `al_nomepai`                   varchar(50) Not Null DEFAULT '',    
 `al_nomemae`                   varchar(50) Not Null DEFAULT '',    
 `al_nomeensino`                varchar(50) Not Null DEFAULT '',    
 `al_localensino`               varchar(80) Not Null DEFAULT '',    
 `al_img`                       varchar(32) NOT NULL DEFAULT '',
 `ocupacao`                     varchar(250)    Not Null DEFAULT 'sem ocupacao de tempos livres',
 `tra_simnao`                   varchar(250)    Not Null DEFAULT 'nao',
 `tra_desc`                     varchar(250)    Not Null DEFAULT 'Sem Transporte',
  PRIMARY KEY (`al_id`)
) ENGINE=myisam DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `EncarregadoDeEducacao` (
 `ee_id`                        int(4) unsigned Not Null AUTO_INCREMENT,
 `ee_nome`                      varchar(50) Not Null DEFAULT '',    
 `ee_parentesco`                varchar(10) Not Null DEFAULT '',    
 `ee_localtrab`                 varchar(80) Not Null DEFAULT '',    
 `ee_telemovel`                 int(9)  Not Null DEFAULT 0,
 `ee_telefone`                  int(9)  Not Null DEFAULT 0,
 `ee_urgencia`                  int(9)  Not Null DEFAULT 0,
 `ee_email`                     varchar(50) Not Null DEFAULT '',
  PRIMARY KEY (`ee_id`)
) ENGINE=myisam DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

I need to select data from the 3 tables to list the data of the full enrollment of the student ( data from the 3 tables ) for example enrollment 1 with student 2 in charge of education5 and the respective data of each of the 3.

if you try to put in 3 select I know that from the error I wanted to know how to do what I want without error, thank you.

  • Helder try to provide more data to your question. How are the tables you are using? What do you need to do? So your question is not clear.

  • Also the last SQL string should not work like this...

  • 1

    The tables Inscricao, Aluno and EncarregadoDeEducacao have the same structure? At first, one can use UNION to bring the three together darlings in one, but one would have to fill the missing columns with NULL if the table structures are different (I don’t know if SQL does it automatically or not). P.S. Your idc is unique among the three tables? If so, how do you guarantee it?

2 answers

0

thanks for your help, When I saw your answer I had already done so :

$sql = "SELECT * FROM Inscricao, Aluno, EncarregadoDeEducacao WHERE Inscricao.al_id = Aluno.al_id AND EncarregadoDeEducacao.ee_id = Inscricao.ee_id AND Inscricao.al_id = ".$_GET['idc'];

I think both of you are

0


You have to join the keys with the table based on what you have in the Registration table:

    select *
    from Inscricoes i
    inner join Aluno a on a.al_id = i.al_id
    inner join EncarregadoDeEducacao e on e.ee_id = i.ee_id
    WHERE a.al_id = SEU_PARAMETRO
    /*
        Aqui você pode continuar o where, order by e etc...
    */

Browser other questions tagged

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