首页 > 解决方案 > 使用 MySQL 查询检索所有“子”项

问题描述

我正在尝试编写一个 SQL 查询来生成一个包含每个“父”(在本例中为元素)的所有“子”项的表。

为了清楚起见,我在下面创建了这个问题的简化实例并设置了一个 db-fiddle 实例。

给定以下虚拟数据:

-- -----------------------------------------------------
-- Table `Element`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Element` (
  `idElement` INT NOT NULL AUTO_INCREMENT,
  `Element_Name` VARCHAR(45) NULL,
  PRIMARY KEY (`idElement`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `Property`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Property` 
(
    `idProperty` INT NOT NULL AUTO_INCREMENT,
    `Property_Text` VARCHAR(45) NULL,
    `Property_Node_ID` INT NULL,
    `Element_idElement` INT NOT NULL,
    PRIMARY KEY (`idProperty`),
    INDEX `fk_Property_Element1_idx` (`Element_idElement` ASC),
    CONSTRAINT `fk_Property_Element1`
        FOREIGN KEY (`Element_idElement`)
        REFERENCES `Element` (`idElement`)
)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Property_2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Property_2` 
(
    `idProperty_2` INT NOT NULL AUTO_INCREMENT,
    `Property_2_Text` VARCHAR(45) NULL,
    `Property_Node_ID` INT NULL,
    `Element_idElement` INT NOT NULL,
    PRIMARY KEY (`idProperty_2`),
    INDEX `fk_Property_2_Element_idx` (`Element_idElement` ASC),
    CONSTRAINT `fk_Property_2_Element`
         FOREIGN KEY (`Element_idElement`)
         REFERENCES `Element` (`idElement`)
)
ENGINE = InnoDB;

INSERT INTO `Element` (`idElement`, `Element_Name`) 
VALUES (NULL, 'element_1');
INSERT INTO `Element` (`idElement`, `Element_Name`) 
VALUES (NULL, 'element_2');

INSERT INTO `Property` (`idProperty`, `Property_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_a', NULL, '1');
INSERT INTO `Property` (`idProperty`, `Property_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_b', NULL, '1');                          
INSERT INTO `Property` (`idProperty`, `Property_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_c', NULL, '2');                              
INSERT INTO `Property_2` (`idProperty_2`, `Property_2_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_2_a', NULL, '1');
INSERT INTO `Property_2` (`idProperty_2`, `Property_2_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_2_b', NULL, '2');
INSERT INTO `Property_2` (`idProperty_2`, `Property_2_Text`, `Property_Node_ID`, `Element_idElement`) 
VALUES (NULL, 'property_2_c', NULL, '2');

我想输出以下内容,其中元素及其属性的每个唯一组合逐行显示。

它们首先按元素排序,然后按第一个属性,最后按最后一个属性:

-------------------------------------
element   | property   | property_2
-------------------------------------
element_1 | property_a | property_2_a
element_1 | property_b | property_2_a
element_2 | property_c | property_2_b
element_2 | property_c | property_2_c

在此处输入图像描述

请参阅 db-fiddle 的链接: https ://www.db-fiddle.com/f/csqTVJFTtpodqPksQtyrbs/0 。任何帮助将不胜感激

标签: mysqlsql

解决方案


我真的不明白这个问题 - 并且命名政策令人困惑 - 但以下内容未能解决哪个部分?

SELECT e.Element_Name element
     , p.Property_text property
     , p2.Property_2_text property_2
  FROM Element e
  JOIN Property p
    ON p.Element_idElement = e.idElement
  JOIN Property_2 p2
    ON p2.Element_idElement = e.idElement
 ORDER
    BY element
     , property
     , property_2; 

推荐阅读