如何使用泛化来连接表

时间:2016-12-23 17:47:48

标签: mysql sql join

我在表上执行JOIN时出现问题:

在报告购物车系统中,我有用户,例如学生,家长和学校员工。我需要生成一个SQL语句,当我输入父母的访问ID时,它会列出与父母ID相关的所有学生

遵循模式:

Model

这是否是实现这种“概括”的最佳方式以及父母和学生之间的这种关系,因为他们都是用户?有人能帮助我吗?

SQL代码:

        -- -----------------------------------------------------
        -- Table `testeboletim`.`type_user`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testeboletim`.`type_user` (
          `idtype_user` INT NOT NULL AUTO_INCREMENT,
          `role` VARCHAR(45) NULL,
          PRIMARY KEY (`idtype_user`))
        ENGINE = InnoDB;


        -- -----------------------------------------------------
        -- Table `testeboletim`.`user`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testeboletim`.`user` (
          `iduser` INT NOT NULL AUTO_INCREMENT,
          `name` VARCHAR(45) NULL,
          `ID` VARCHAR(20) NULL,
          `birth` DATE NULL,
          `telephone` VARCHAR(20) NULL,
          `phone` VARCHAR(20) NULL,
          `email` VARCHAR(45) NULL,
          `type_user_idtype_user` INT NOT NULL,
          PRIMARY KEY (`iduser`, `type_user_idtype_user`),
          INDEX `fk_usuario_tipo_usuario_idx` (`type_user_idtype_user` ASC),
          CONSTRAINT `fk_usuario_tipo_usuario`
            FOREIGN KEY (`type_user_idtype_user`)
            REFERENCES `testeboletim`.`type_user` (`idtype_user`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB;


        -- -----------------------------------------------------
        -- Table `testeboletim`.`student`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testeboletim`.`student` (
          `idstudent` INT NOT NULL AUTO_INCREMENT,
          `user_iduser` INT NOT NULL,
          `user_type_user_idtype_user` INT NOT NULL,
          PRIMARY KEY (`idstudent`, `user_iduser`, `user_type_user_idtype_user`),
          INDEX `fk_aluno_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
          CONSTRAINT `fk_aluno_usuario1`
            FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
            REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB;


        -- -----------------------------------------------------
        -- Table `testeboletim`.`parents`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testeboletim`.`parents` (
          `idparents` INT NOT NULL AUTO_INCREMENT,
          `user_iduser` INT NOT NULL,
          `user_type_user_idtype_user` INT NOT NULL,
          PRIMARY KEY (`idparents`, `user_iduser`, `user_type_user_idtype_user`),
          INDEX `fk_responsavel_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
          CONSTRAINT `fk_responsavel_usuario1`
            FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
            REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB;


        -- -----------------------------------------------------
        -- Table `testeboletim`.`student_has_parents`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `testeboletim`.`student_has_parents` (
          `student_idstudent` INT NOT NULL,
          `student_user_iduser` INT NOT NULL,
          `parents_idparents` INT NOT NULL,
          `parents_user_iduser` INT NOT NULL,
          PRIMARY KEY (`student_idstudent`, `student_user_iduser`, `parents_idparents`, `parents_user_iduser`),
          INDEX `fk_aluno_has_responsavel_responsavel1_idx` (`parents_idparents` ASC, `parents_user_iduser` ASC),
          INDEX `fk_aluno_has_responsavel_aluno1_idx` (`student_idstudent` ASC, `student_user_iduser` ASC),
          CONSTRAINT `fk_aluno_has_responsavel_aluno1`
            FOREIGN KEY (`student_idstudent` , `student_user_iduser`)
            REFERENCES `testeboletim`.`student` (`idstudent` , `user_iduser`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION,
          CONSTRAINT `fk_aluno_has_responsavel_responsavel1`
            FOREIGN KEY (`parents_idparents` , `parents_user_iduser`)
            REFERENCES `testeboletim`.`parents` (`idparents` , `user_iduser`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB;


        SET SQL_MODE=@OLD_SQL_MODE;
        SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
        SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,我们需要查看testeboletimstudenttesteboletimparentstesteboletim。{{ 1}}或student_has_parentstesteboletim。我决定使用usertesteboletim解决您的问题,因为它在引用键方面更清晰,而不是。

使用usertesteboletim

的解决方案

根据您的问题,我们正在查找usertesteboletim中的所有行,这些行具有相应的student idusertesteboletim基于user的{​​{1}}。user_iduser

- SQL定义:

testeboletim

现在要对parents执行相同的操作,需要使用SELECT * FROM `testeboletim`.`student` WHERE `user_iduser` IN (SELECT DISTINCT(`iduser`) FROM `testeboletim`.`user` WHERE `iduser` IN (SELECT DISTINCT(`user_iduser`) FROM `testeboletim`.`parents`) ); ;在这种情况下JOINLEFT JOIN

testeboletim

由于我没有任何价值观,我将与您分享解释,以“证明”该查询有效。

student