Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用泛化连接表_Mysql_Sql_Join - Fatal编程技术网

Mysql 如何使用泛化连接表

Mysql 如何使用泛化连接表,mysql,sql,join,Mysql,Sql,Join,在表上执行联接时遇到问题问题问题是: 在报告车系统中,我有用户,例如学生、家长和学校员工。我需要生成一个SQL语句,当我输入家长的访问ID时,它会列出与家长ID相关的所有学生 遵循以下模式: 既然家长和学生都是用户,这是实现这种“泛化”和家长与学生之间关系的最佳方式吗?有人能帮我吗 SQL代码: -- ----------------------------------------------------- -- Table `testeboletim`.`ty

在表上执行联接时遇到问题问题问题是:

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

遵循以下模式:

既然家长和学生都是用户,这是实现这种“泛化”和家长与学生之间关系的最佳方式吗?有人能帮我吗

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;

为了解决这个问题,我们需要查看
testeboletim
student
testeboletim
家长
,或者
testeboletim
学生有家长或者
testeboletim
用户。我决定使用
testeboletim
user
来解决您的问题,因为它在引用键方面更清晰,而在其他方面则不然

使用
testeboletim
解决方案
用户

根据您的问题,我们正在查找
testeboletim
student
中的所有行,这些行具有相应的
iduser
testeboletim
用户
基于
testeboletim
家长的
user\iduser

--SQL定义:

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

SELECT * FROM `testeboletim`.`student` AS `student`
LEFT JOIN `testeboletim`.`user` AS `user` 
ON `student`.`user_iduser` = `user`.`iduser`
LEFT JOIN `testeboletim`.`parents` AS `parents`
ON `user`.`iduser` = `parents`.`user_iduser`;
因为我没有任何值,所以我将与您分享解释,以“证明”查询是有效的

mysql>  SELECT * FROM `testeboletim`.`student` AS `student`
    ->     LEFT JOIN `testeboletim`.`user` AS `user` 
    ->     ON `student`.`user_iduser` = `user`.`iduser`
    ->     LEFT JOIN `testeboletim`.`parents` AS `parents`
    ->     ON `user`.`iduser` = `parents`.`user_iduser`;
Empty set (0.01 sec)

mysql> EXPLAIN  SELECT * FROM `testeboletim`.`student` AS `student`
    ->     LEFT JOIN `testeboletim`.`user` AS `user` 
    ->     ON `student`.`user_iduser` = `user`.`iduser`
    ->     LEFT JOIN `testeboletim`.`parents` AS `parents`
    ->     ON `user`.`iduser` = `parents`.`user_iduser`;
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
| id   | select_type | table   | type  | possible_keys               | key                         | key_len | ref                      | rows | Extra                                           |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
|    1 | SIMPLE      | student | index | NULL                        | PRIMARY                     | 12      | NULL                     |    1 | Using index                                     |
|    1 | SIMPLE      | user    | ALL   | PRIMARY                     | NULL                        | NULL    | NULL                     |    1 | Using where; Using join buffer (flat, BNL join) |
|    1 | SIMPLE      | parents | ref   | fk_responsavel_usuario1_idx | fk_responsavel_usuario1_idx | 4       | testeboletim.user.iduser |    1 | Using where; Using index                        |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
3 rows in set (0.00 sec)

如果
user
表中有区别于父母和学生的东西(可能
type\u user
已经有了),那么就不需要单独的
父母和
student
表。在这种情况下,每个
学生都有父母
只需对
用户
有两个单独的引用即可。我所建议的方法的唯一缺点是,纯粹在MySQL中,确保
用户
不会在关系中处于不适当的一方并不是很简单;但如果不让不合适的用户进入我建议的表格,您也会遇到类似的问题。