"Christian Anton" <christiananton@hotmail.com> wrote in message
news:3b3ab3aa_1@newsa.ev1.net...
> I have a table with a list of marbles, each marble has three colors in it
> (color1, color2, color3) and a serial number. In another table I have
eight
> colors to choose from but the list of colors grows regularly.
> How do I associate a marble with three colors from the color table (a
marble
> may have three red sides or red-yellow-blue)?
Not sure what you mean exactly, but the following:
CREATE TABLE marbles ( m_id serial, m_col1 int4, m_col2 int4, m_col3 int4
);
CREATE TABLE colours ( c_id serial, c_name text
);
INSERT INTO colours (c_name) VALUES ('red');
INSERT INTO colours (c_name) VALUES ('green');
INSERT INTO colours (c_name) VALUES ('blue');
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (1,1,1);
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (1,2,3);
INSERT INTO marbles (m_col1,m_col2,m_col3) VALUES (3,2,1);
SELECT m.m_id, c1.c_name as colname1, c2.c_name as colname2, c3.c_name as
colname3
FROM marbles m
JOIN colours c1 ON m.m_col1 = c1.c_id
JOIN colours c2 ON m.m_col2 = c2.c_id
JOIN colours c3 ON m.m_col3 = c3.c_id;
Will produce:
m_id | colname1 | colname2 | colname3
------+----------+----------+---------- 1 | red | red | red 3 | blue | green | red 2 | red
|green | blue
We join the colours table to the marbles table three times and need to alias
it differently each time. Otherwise, we don't know which colour-name matches
which colour-code.
If it's not that simple, can we have table definitions and an example of the
sort of output you'd like?
- Richard Huxton