选择并连接所有表

SELECTING and Joining all tables

你好有人可以帮助我如何加入所有这些关于 serial_number 的表,我使用示例 "where name2="mynameis2";"我只能加入两个或 3 个表,但如果我 select 所有表我就做不到。

ERD 示例。

SQL 脚本:

     SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`table1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table1` (
  `idtable1` INT NOT NULL,
  `name1` VARCHAR(45) NULL,
  PRIMARY KEY (`idtable1`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table6`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table6` (
  `idtable6` INT NOT NULL,
  `name6` VARCHAR(45) NULL,
  PRIMARY KEY (`idtable6`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table2` (
  `idtable2` INT NOT NULL,
  `name2` VARCHAR(45) NULL,
  `idtable1` INT NOT NULL,
  `idtable6` INT NOT NULL,
  PRIMARY KEY (`idtable2`),
  INDEX `fk_table2_table1_idx` (`idtable1` ASC),
  INDEX `fk_table2_table61_idx` (`idtable6` ASC),
  CONSTRAINT `fk_table2_table1`
    FOREIGN KEY (`idtable1`)
    REFERENCES `mydb`.`table1` (`idtable1`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_table2_table61`
    FOREIGN KEY (`idtable6`)
    REFERENCES `mydb`.`table6` (`idtable6`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table3`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table3` (
  `idtable3` INT NOT NULL,
  `name3` VARCHAR(45) NULL,
  PRIMARY KEY (`idtable3`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table4`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table4` (
  `idtable4` INT NOT NULL,
  `name4` VARCHAR(45) NULL,
  `idtable2` INT NOT NULL,
  `idtable3` INT NOT NULL,
  PRIMARY KEY (`idtable4`),
  INDEX `fk_table4_table21_idx` (`idtable2` ASC),
  INDEX `fk_table4_table31_idx` (`idtable3` ASC),
  CONSTRAINT `fk_table4_table21`
    FOREIGN KEY (`idtable2`)
    REFERENCES `mydb`.`table2` (`idtable2`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_table4_table31`
    FOREIGN KEY (`idtable3`)
    REFERENCES `mydb`.`table3` (`idtable3`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table7`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table7` (
  `idtable7` INT NOT NULL,
  `name7` VARCHAR(45) NULL,
  PRIMARY KEY (`idtable7`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`table5`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table5` (
  `idtable5` INT NOT NULL,
  `name5` VARCHAR(45) NULL,
  `idtable2` INT NOT NULL,
  `idtable7` INT NOT NULL,
  PRIMARY KEY (`idtable5`),
  INDEX `fk_table5_table21_idx` (`idtable2` ASC),
  INDEX `fk_table5_table71_idx` (`idtable7` ASC),
  CONSTRAINT `fk_table5_table21`
    FOREIGN KEY (`idtable2`)
    REFERENCES `mydb`.`table2` (`idtable2`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_table5_table71`
    FOREIGN KEY (`idtable7`)
    REFERENCES `mydb`.`table7` (`idtable7`)
    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;

尝试

select * from table1 t1 
join table2 t2 on t1.idtable1 = t2.idtable1
join table4 t4 on t2.idtable2 = t4.idtable2
join table3 t3 on t4.idtable3 = t3.idtable3
join table5 t5 on t2.idtable2 = t5.idtable2
join table6 t6 on t2.idtable6 = t6.idtable6
join table7 t7 on t5.idtable7 = t7.idtable7