我有这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'表上,如何构建查询以返回基于的名称每个领域的身份证?
示例:
在计算机桌上,我有一台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
答案 0 :(得分: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 ...
如果您愿意,可以命名不同的i1
JOINS,而不是使用i2
inventory
,以便更容易区分它们(AS 'iCaixa', AS 'iMobo'
等)。