你實際上想要正常化一點。退房http://en.wikipedia.org/wiki/First_normal_form
我認爲你正在做的是:
Beer
BeerID BeerName BeerManufacturer
------ -------- ----------------
1 Beer1 Brewer 1
2 Beer2 Brewer 1
3 Beer3 Brewer 2
4 Beer4 Brewer 3
AltBeer
AltBeerID BeerID BeerID1 BeerID2 BeerID3
--------- ------ ------- ------- -------
1 1 3 4
2 2 5
3 4 7
相反,你要只有一個列備用啤酒。在這種情況下,AltBeer成爲
AltBeer
AltBeerID BeerID AltBeer
--------- ------ -------
1 1 3
1 1 4
2 2 5
3 4 7
然後做啤酒和AltBeer(基於BeerID)聯接會給你類似於原來的啤酒BeersID的列表。您可以將備用啤酒再次與啤酒加盟細節(基於BeerID和AltBeer)
SELECT beer.beermanufacturer,
beer.beername,
beer_1.beername AS recbeer,
beer_1.beermanufacturer AS recbeerman
FROM (altbeer
INNER JOIN beer
ON altbeer.beerid = beer.beerid)
INNER JOIN beer AS beer_1
ON altbeer.altbeer = beer_1.beerid;
的結果是這樣的(沒有DUP的,保證的廣告)
BeerName BeerManufacturer RecBeer RecBeerMan
-------- ---------------- ------- -----------
Beer1 BeerMan1 Beer3 BeerMan2
Beer1 BeerMan1 Beer4 BeerMan3
(當然,如果我們正確地將這一切正常化,我們還將啤酒製造商分成了自己的表格,因爲啤酒和製造商之間存在着多對一的關係。)