Hello,
I have the following database:
CREATE TABLE sectors (sec_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, sec_name CHAR(25));
INSERT INTO sectors (sec_id, sec_name) VALUES (NULL, 'Spinward Marches');
CREATE TABLE subsectors (sub_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, sec_home INT, sub_name CHAR(25));
INSERT INTO subsectors (sub_id, sec_home, sub_name) VALUES (NULL, 1, 'Querion');
CREATE TABLE worlds (wld_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, sec_home INT, subsec_home INT, name CHAR(25), location Char(4), uwp CHAR(8), trade CHAR(15), cap CHAR(3), ex_uwp CHAR(3), alleg CHAR(2) );
CREATE TABLE stars ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, star_home INT, star_num INT, type CHAR(1), spectral INT, size Char(4) );
CREATE TABLE bases ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, base_home INT, base CHAR(3) );
INSERT INTO worlds (wld_id, sec_home, subsec_home, name, location, uwp, trade, cap, ex_uwp, alleg) VALUES (NULL, (SELECT sec_id FROM sectors WHERE sec_name='Spinward Marches'), (SELECT sub_id FROM subsectors WHERE sub_name='Chronor'),'Zeycude', '0101', 'C3306989', '000400000ABC000', NULL, '613', 'Zh' );
INSERT INTO stars (id, star_home, star_num, type, spectral, size) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0101'), 1, 'K', '9', 'V' ); INSERT INTO bases (id, base_home, base) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0101'), 'I' ); INSERT INTO bases (id, base_home, base) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0101'), 'Z' );
INSERT INTO worlds (wld_id, sec_home, subsec_home, name, location, uwp, trade, cap, ex_uwp, alleg)
VALUES (NULL, (SELECT sec_id FROM sectors WHERE sec_name='Spinward Marches'), (SELECT sub_id FROM subsectors WHERE sub_name='Chronor'), 'Reno', '0102', 'C1207B9A', '000400000AB0000', NULL, '603', 'Zh' );
INSERT INTO stars (id, star_home, star_num, type, spectral, size) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0102'), 1, 'G', '8', 'V' );
INSERT INTO stars (id, star_home, star_num, type, spectral, size) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0102'), 2, 'M', '1', 'D' );
INSERT INTO bases (id, base_home, base) VALUES ( NULL, (SELECT wld_id FROM worlds WHERE location='0102'), NULL );
When I use the select:
SELECT sec_name, sub_name, location, name, uwp, base, trade, cap, ex_uwp, alleg, type, spectral, size FROM sectors s JOIN subsectors u ON s.sec_id=u.sec_home JOIN worlds w ON w.sec_home=s.sec_id JOIN stars t ON t.star_home=wld_id JOIN bases b ON b.base_home=wld_id WHERE sub_name='Chronor';
I get:
| sec_name | sub_name | location | name | uwp | base | trade | cap | ex_uwp | alleg | type | spectral | size |
+------------------+----------+----------+---------+----------+------+-----------------+------+--------+-------+------+----------+------+
| Spinward Marches | Chronor | 0101 | Zeycude | C3306989 | I | 000400000ABC000 | NULL | 613 | Zh | K | 9 | V |
| Spinward Marches | Chronor | 0101 | Zeycude | C3306989 | Z | 000400000ABC000 | NULL | 613 | Zh | K | 9 | V |
| Spinward Marches | Chronor | 0102 | Reno | C1207B9A | NULL | 000400000AB0000 | NULL | 603 | Zh | G | 8 | V |
| Spinward Marches | Chronor | 0102 | Reno | C1207B9A | NULL | 000400000AB0000 | NULL | 603 | Zh | M | 1 | D
What I'd like is:
| sec_name | sub_name | location | name | uwp | base | base 2(if exist) | trade | cap | ex_uwp | alleg | type | spectral | size | type 2(if exist) | spectral 2(if exist) | size 2(if exist) |
+------------------+----------+----------+---------+----------+------+-----------------+------+--------+-------+------+----------+------+
**| Spinward Marches | Chronor | 0101 | Zeycude | C3306989 | I | Z | **
000400000ABC000 | NULL | 613 | Zh | K | 9 | V |
| Spinward Marches | Chronor | 0102 | Reno | C1207B9A | NULL | 000400000AB0000 | NULL | 603 | Zh | G | 8 | V | M | 1 | D
But I can't figure the SQL.
Can someone help me?
Thanks in advance!