mysql完全连接4个表

qgelzfjb  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(238)

我需要一些帮助来构建一个sql,以便在四个表上获取类似“完全外部连接”的内容。我有这个结构,不能对它做太多修改,因为它是一个已经在使用的数据库:

-- ----------------------------
-- Table structure for article
-- ----------------------------
CREATE TABLE `article`  (
  `ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NULL DEFAULT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `article` VALUES (1, 'Coffeemaker');
INSERT INTO `article` VALUES (2, 'Toaster');

-- ----------------------------
-- Table structure for language
-- ----------------------------
CREATE TABLE `language`  (
  `ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NULL DEFAULT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `language` VALUES (1, 'German');
INSERT INTO `language` VALUES (2, 'English');

-- ----------------------------
-- Table structure for property
-- ----------------------------
CREATE TABLE `property`  (
  `ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NULL DEFAULT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `property` VALUES (1, 'DescriptionText');
INSERT INTO `property` VALUES (2, 'EAN-Code');

-- ----------------------------
-- Table structure for data
-- ----------------------------
CREATE TABLE `data`  (
  `ArticleID` int(10) UNSIGNED NOT NULL,
  `PropertyID` int(10) UNSIGNED NOT NULL,
  `LanguageID` int(10) UNSIGNED NOT NULL,
  `Value` varchar(255) NULL DEFAULT NULL,
  PRIMARY KEY (`ArticleID`, `PropertyID`, `LanguageID`) USING BTREE,
  CONSTRAINT `FK_ArticleID` FOREIGN KEY (`ArticleID`) REFERENCES `article` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_LanguageID` FOREIGN KEY (`LanguageID`) REFERENCES `language` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_PropertyID` FOREIGN KEY (`PropertyID`) REFERENCES `property` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;

INSERT INTO `data` VALUES (1, 1, 1, 'Eine Kaffemaschine');
INSERT INTO `data` VALUES (2, 1, 2, 'A toaster');

sql语句:http://sqlfiddle.com/#!9/91dc8/1号
我想得到的是一个新视图,它包含一个对所有实体表的连接,显示所有项目、所有属性和所有语言的行,但如果可用,则使用现有的数据,如果不可用,则使用null。

有可能吗?sql是什么样子的?

xuo3flqw

xuo3flqw1#

你好像在找这个。。。

SELECT a.id articleid
     , p.id propertyid
     , l.id languageid
     , d.value 
  FROM article a
 CROSS -- optional keyword
  JOIN property p
 CROSS -- optional keyword
  JOIN language l 
  LEFT -- not optional
  JOIN data d
    ON d.articleid = a.id
   AND d.propertyid = p.id
   AND d.languageid = l.id;

+-----------+------------+------------+--------------------+
| articleid | propertyid | languageid | value              |
+-----------+------------+------------+--------------------+
|         1 |          1 |          1 | Eine Kaffemaschine |
|         2 |          1 |          1 | NULL               |
|         1 |          2 |          1 | NULL               |
|         2 |          2 |          1 | NULL               |
|         1 |          1 |          2 | NULL               |
|         2 |          1 |          2 | A toaster          |
|         1 |          2 |          2 | NULL               |
|         2 |          2 |          2 | NULL               |
+-----------+------------+------------+--------------------+
unftdfkk

unftdfkk2#

如果你想要所有的文章,你不想“完全加入”。你想要一个 left join 首先是 articles table。此外,您甚至不需要这样做,因为您的所有文章都有数据中的值。
但问题确实明确了所有条款,因此:

SELECT a.*, p.*, l.*, d.*
FROM article a LEFT JOIN
     data d 
     ON d.ArticleID = a.ID LEFT JOIN
     property p
     ON d.PropertyID = p.id LEFT JOIN
     language l
     ON d.LanguageID = l.id;

我不知道你所说的所有文章和所有语言是什么意思(我第一次读这个问题时错过了第二部分)。如果需要所有组合,则:

SELECT a.*, p.*, l.*, d.*
FROM article a CROSS JOIN
     languages l LEFT JOIN
     data d 
     ON d.ArticleID = a.ID AND
        d.LanguageID = l.ID LEFT JOIN
     property p
     ON d.PropertyID = p.id ;

相关问题