I am working with this schema: http://classes.engr.oregonstate.edu/eecs/winter2013/cs275-400/tester/bsgSchema.html
and I am trying to achieve: Find the fname, lname and ship_instance id of all people who do not have Viper certificaton but are assigned to at least one instance of a Viper class ship (this includes all variants of Viper class ships). Return a row for every ship/person combination.)
I am close. I have written the following query:
SELECT DISTINCT p.fname, p.lname, si.id from bsg_people p
INNER JOIN bsg_ship_assignment sa ON sa.pid = p.id
INNER JOIN bsg_ship_class sc ON sc.id = sa.cid
INNER Join bsg_ship_instance si ON si.class = sa.cid
WHERE p.id NOT
IN (
SELECT cp.pid
FROM bsg_cert_people cp
INNER JOIN bsg_cert c ON c.id = cp.cid
WHERE cp.cid = '2'
)
AND sc.name = 'viper'
My query returns a number of extra instances.
SELECT p.fname
, p.lname
, sk.id
FROM bsg_people p
LEFT
JOIN bsg_cert_people cp
ON cp.pid = p.id
LEFT
JOIN bsg_cert c
ON c.id = cp.cid
AND c.title = 'viper'
JOIN bsg_ship_assignment ps
ON ps.pid = p.id
JOIN bsg_ship_instance sk
ON sk.id = ps.sid
AND sk.class = ps.cid
JOIN bsg_ship_class k
ON k.id = sk.class
WHERE k.name = 'viper'
AND c.id IS NULL;
+---------+----------+------+
| fname | lname | id |
+---------+----------+------+
| William | Adama | 8757 |
| Lee | Adama | 121 |
| Lee | Adama | 289 |
| Lee | Adama | 1104 |
| Laura | Roslin | 2343 |
| Gaius | Baltar | 289 |
| Samuel | Anders | 3203 |
| Samuel | Anders | 8757 |
| Brendan | Costanza | 7242 |
| Brendan | Costanza | 2343 |
+---------+----------+------+
SELECT bsg_people.fname, bsg_people.lname, bsg_ship_instance.id FROM bsg_people
INNER JOIN bsg_ship_assignment ON bsg_ship_assignment.pid = bsg_people.id
INNER JOIN bsg_ship_instance ON bsg_ship_assignment.sid = bsg_ship_instance.id
INNER JOIN bsg_ship_class ON bsg_ship_class.id = bsg_ship_instance.class
WHERE bsg_people.id NOT IN
(
SELECT bsg_cert_people.pid FROM bsg_cert_people
INNER JOIN bsg_cert ON bsg_cert_people.cid = bsg_cert.id
WHERE bsg_cert.title = 'Viper'
)
AND bsg_ship_class.name = 'viper'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.