我正在使用此架构:http://classes.engr.oregonstate.edu/eecs/winter2013/cs275-400/tester/bsgSchema.html
我想要实现:找到所有没有Viper证书但被分配到Viper类船的至少一个实例的人的fname,lname和ship_instance id(这包括Viper级船的所有变体) 。为每个船/人组合返回一行。)
我很接近。我写了以下查询:
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'
我的查询返回了一些额外的实例。
答案 0 :(得分:1)
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 |
+---------+----------+------+
答案 1 :(得分:1)
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'