-1

我有这3张桌子:

-- ----------------------------
-- Table structure for computers
-- ----------------------------
DROP TABLE IF EXISTS `computers`;
CREATE TABLE `computers`  (
  `id_pc` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `case` int(11) NOT NULL,
  `mobo` int(11) NOT NULL,
  `cpu` int(11) NOT NULL,
  `gpu` int(11) NOT NULL,
  `psu` int(11) NOT NULL,
  `ram` int(11) NOT NULL,
  `ssd` int(11) NOT NULL,
  `hdd` int(11) NOT NULL,
  PRIMARY KEY (`id_pc`) USING BTREE,
  INDEX `fk_case`(`case`) USING BTREE,
  INDEX `fk_mobo`(`mobo`) USING BTREE,
  INDEX `fk_cpu`(`cpu`) USING BTREE,
  INDEX `fk_gpu`(`gpu`) USING BTREE,
  INDEX `fk_psu`(`psu`) USING BTREE,
  INDEX `fk_ram`(`ram`) USING BTREE,
  INDEX `fk_ssd`(`ssd`) USING BTREE,
  INDEX `fk_hdd`(`hdd`) USING BTREE,
  CONSTRAINT `fk_case` FOREIGN KEY (`case`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_cpu` FOREIGN KEY (`cpu`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_gpu` FOREIGN KEY (`gpu`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_hdd` FOREIGN KEY (`hdd`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_mobo` FOREIGN KEY (`mobo`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_psu` FOREIGN KEY (`psu`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_ram` FOREIGN KEY (`ram`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_ssd` FOREIGN KEY (`ssd`) REFERENCES `inventory` (`id_inventario`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for hardware_types
-- ----------------------------
DROP TABLE IF EXISTS `hardware_types`;
CREATE TABLE `hardware_types`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `id`(`id`) USING BTREE,
  INDEX `id_2`(`id`, `type_name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for inventory
-- ----------------------------
DROP TABLE IF EXISTS `inventory`;
CREATE TABLE `inventory`  (
  `id_inventario` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `tipo` int(255) NOT NULL,
  `modelo` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `serial_number` int(11) NOT NULL,
  PRIMARY KEY (`id_inventario`) USING BTREE,
  INDEX `fk_tipo`(`tipo`) USING BTREE,
  CONSTRAINT `fk_tipo` FOREIGN KEY (`tipo`) REFERENCES `hardware_types` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

在我的 c# 程序中,我需要获取一个包含“computers”表内容的 DataGridView,但是有些字段是外键,并且数据在“inventory”表中,如何构建查询以根据 id 返回名称每个领域?

例子:

在计算机表上,我有一台 CPU id 为 1 和 GPU id 为 2 的计算机,我怎样才能返回每个字段,而不是我得到名称的 id?

我已经尝试使用 INNER JOIN 做一些事情,但没有成功。

我试过的一个总是返回空的例子:

SELECT c.*, i.nome AS item_name
FROM
computers AS c
INNER JOIN inventory AS i ON i.id_inventario = c.caixa AND i.id_inventario = c.mobo AND i.id_inventario = c.cpu AND i.id_inventario = c.gpu AND i.id_inventario = c.psu AND i.id_inventario = c.ram AND i.id_inventario = c.ssd AND i.id_inventario = c.hdd

电脑表数据:

id_pc   nome        case    mobo    cpu gpu psu ram ssd hdd
1       Teste213    1       2       1   1   1   2   2   2

库存表数据:

id_inventario   nome    tipo    modelo      serial_number
1               Teste   0       teste       123
2               Teste2  1       1testesa    1234

在获取计算机列上的所有行时,我需要获取列“nome”,例如:

id_pc   nome        case    mobo    cpu     gpu     psu     ram     ssd     hdd
1       Teste213    Teste   Teste2  Teste   Teste   Teste   Teste2  Teste2  Teste2
4

1 回答 1

1

您需要多个 JOIN,每列 1 个:

SELECT c.id_pc, c.nome, i1.nome AS 'case', i2.nome AS 'mobo'
FROM computers AS c
INNER JOIN inventory AS i1
ON i1.id_inventario = c.case
INNER JOIN inventory AS i2
ON i2.id_inventario = c.mobo
INNER JOIN ...

在此处查看 SQLFiddle。

如果您愿意,而不是使用i1, i2,您可以命名不同的inventoryJOINS 以更容易区分它们(AS 'iCaixa', AS 'iMobo'等)。

于 2018-05-25T15:26:38.733 回答