我有三个表这样描述:
+----------+ +-----------------+ +----------+
| products | | products_stores | | stores |
+----------+ +-----------------+ +----------+
| barecode | | #barecode | | storeID |
| name |----| #storeID |----| location |
+----------+ | price | +----------+
+-----------------+
像这样创建它们:
CREATE TABLE IF NOT EXISTS `products` (
`barecode` varchar(100) NOT NULL UNIQUE,
`name` varchar(25) NOT NULL,
PRIMARY KEY (`barecode`)
);
CREATE TABLE IF NOT EXISTS `stores` (
`idStore` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`location` varchar(100) NOT NULL,
PRIMARY KEY (`idStore`)
);
CREATE TABLE IF NOT EXISTS `products_stores` (
`idStore` int(10) NOT NULL,
`barecode` VARCHAR(100) NOT NULL,
`price` double NOT NULL,
FOREIGN KEY (`barecode`) REFERENCES `products`(`barecode`),
FOREIGN KEY (`idStore`) REFERENCES `stores`(`idStore`)
);
我想在正确的商店获得所有产品及其价格,所以我尝试了 NATURAL JOIN,但它什么也没返回(代码低于 1-)。所以我只尝试了一个 NATURAL JOIN 来检查它是否有效(代码低于 2- 和 3-)。
1- SELECT * FROM products NATURAL JOIN products_stores NATURAL JOIN stores;
2- SELECT * FROM products JOIN products_stores NATURAL JOIN stores;
3- SELECT * FROM products NATURAL JOIN products_stores JOIN stores;
我不明白为什么“双重”自然连接不起作用。有谁可以帮助我解决这个问题?谢谢。