【发布时间】:2019-07-08 02:15:00
【问题描述】:
不知道该怎么称呼它,--我确定这必须在其他地方问。
考虑以下,例如。
CREATE TABLE abbrv (
abbrv_id int unsigned primary key not null auto_increment,
usps_primary varchar(64) not null,
usps_preferred varchar(16) not null
);
CREATE TABLE abbrv_variation (
variation_id int unsigned primary key not null auto_increment,
abbrv_id int unsigned, -- FK
variation varchar(64) not null
);
INSERT INTO abbrv(usps_primary, usps_preferred) VALUES('North', 'N');
INSERT INTO abbrv(usps_primary, usps_preferred) VALUES('South', 'S');
INSERT INTO abbrv(usps_primary, usps_preferred) VALUES('East', 'E');
INSERT INTO abbrv(usps_primary, usps_preferred) VALUES('West', 'W');
INSERT INTO `abbrv` (usps_primary, usps_preferred) values('ALLEY', 'ALY');
SET @abbrvId = LAST_INSERT_ID();
INSERT INTO `abbrv_variation` (abbrv_id, variation) VALUES(@abbrvId, 'ALLEE');
INSERT INTO `abbrv_variation` (abbrv_id, variation) VALUES(@abbrvId, 'ALLY');
INSERT INTO `abbrv` (usps_primary, usps_preferred) values('ANEX', 'ANX');
SET @abbrvId = LAST_INSERT_ID();
INSERT INTO `abbrv_variation` (abbrv_id, variation) VALUES(@abbrvId, 'ANNEX');
INSERT INTO `abbrv_variation` (abbrv_id, variation) VALUES(@abbrvId, 'ANNX');
INSERT INTO `abbrv_variation` (abbrv_id, variation) VALUES(@abbrvId, 'ANX');
我只是想做的是返回如下内容:
+--------------+----------------+-----------+
| usps_primary | usps_preferred | variation |
+--------------+----------------+-----------+
| North | N | North | <-- 1 row for "usps_primary"
| North | N | N | <-- 1 row for "usps_preferred"
| South | S | South | <-- 1 row for "usps_primary"
| South | S | S | <-- 1 row for "usps_preferred"
| East | E | East | <-- 1 row for "usps_primary"
| East | E | E | <-- 1 row for "usps_preferred"
| West | W | West | <-- 1 row for "usps_primary"
| West | W | W | <-- 1 row for "usps_preferred"
| ALLEY | ALY | ALLEY | <-- 1 row for "usps_primary"
| ALLEY | ALY | ALY | <-- 1 row for "usps_preferred"
| ALLEY | ALY | ALLEE | X-- one row for each
| ALLEY | ALY | ALLY | X-- variation
| ANEX | ANX | ANEX | <-- 1 row for "usps_primary"
| ANEX | ANX | ANX | <-- 1 row for "usps_preferred"
| ANEX | ANX | ANNEX | X-- one row for each
| ANEX | ANX | ANNX | X-- variation
顺序无关紧要 - 只是它们都在那里。最初,此表仅包含其中每一个的“变体”行,但它变得无法维护,因为它是一个非常大的表,以确保每次添加新变体时都输入所有这些行。
我当然尝试过简单的 LEFT JOIN、CROSS JOIN、OUTER JOIN、左连接和右连接 - 我真的不知道这里的适当做法是什么。
交叉连接似乎大致正确,但我不想要整个系列,每个只有 2 次。
【问题讨论】:
标签: mysql join cross-join