不确定该怎么称呼,-我确定必须在其他地方询问。
请考虑以下内容,例如
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倍。
答案 0 :(得分:1)
l
输出:
SELECT usps_primary, usps_preferred, usps_primary AS variation FROM abbrv
UNION
SELECT usps_primary, usps_preferred, usps_preferred FROM abbrv
UNION
SELECT a.usps_primary, a.usps_preferred, v.variation FROM abbrv AS a
INNER JOIN abbrv_variation AS v USING (abbrv_id)
ORDER BY usps_primary
答案 1 :(得分:0)
您可以使用LATERAL
:
SELECT usps_primary, usps_preferred, s.variation
FROM abbrv a
,LATERAL(SELECT a.usps_primary AS variation
UNION SELECT a.usps_preferred
UNION SELECT av.variation FROM abbrv_variation av WHERE a.abbrv_id = av.abbrv_id) s
输出:
+---------------+-----------------+-----------+
| usps_primary | usps_preferred | variation |
+---------------+-----------------+-----------+
| North | N | North |
| North | N | N |
| South | S | South |
| South | S | S |
| East | E | East |
| East | E | E |
| West | W | West |
| West | W | W |
| ALLEY | ALY | ALLEY |
| ALLEY | ALY | ALY |
| ALLEY | ALY | ALLEE |
| ALLEY | ALY | ALLY |
| ANEX | ANX | ANEX |
| ANEX | ANX | ANX |
| ANEX | ANX | ANNEX |
| ANEX | ANX | ANNX |
+---------------+-----------------+-----------+