我是SQL新手。我有一个查询,我无法得到结果。
从下表中,我需要列出飞行“阿波罗”和“航天飞机”任务(项目名称)的宇航员(astrno)。
projectname | missionno | astrono | role
--------------+-----------+---------+----------------------
Apollo | 1 | 22 | Commander
Apollo | 1 | 42 | Command Module Pilot
Apollo | 1 | 10 | Lunar Module Pilot
Apollo | 7 | 33 | Commander
Apollo | 7 | 16 | Command Module Pilot
Apollo | 7 | 14 | Lunar Module Pilot
Apollo | 8 | 5 | Commander
Apollo | 8 | 27 | Command Module Pilot
Apollo | 8 | 2 | Lunar Module Pilot
Apollo | 9 | 29 | Commander
Apollo | 9 | 36 | Command Module Pilot
Apollo | 9 | 35 | Lunar Module Pilot
Apollo | 10 | 39 | Commander
Apollo | 10 | 44 | Command Module Pilot
Apollo | 10 | 9 | Lunar Module Pilot
Apollo | 11 | 3 | Commander
Apollo | 11 | 11 | Command Module Pilot
Apollo | 11 | 1 | Lunar Module Pilot
Apollo | 12 | 12 | Commander
Apollo | 12 | 21 | Command Module Pilot
Apollo | 12 | 4 | Lunar Module Pilot
Apollo | 13 | 27 | Commander
Apollo | 13 | 40 | Command Module Pilot
Apollo | 13 | 23 | Lunar Module Pilot
Apollo | 14 | 37 | Commander
Apollo | 14 | 32 | Command Module Pilot
Apollo | 14 | 30 | Lunar Module Pilot
Apollo | 15 | 43 | Command Module Pilot
Apollo | 15 | 24 | Lunar Module Pilot
Apollo | 16 | 44 | Commander
Apollo | 16 | 28 | Command Module Pilot
Apollo | 16 | 15 | Lunar Module Pilot
Apollo | 17 | 9 | Commander
Apollo | 17 | 17 | Command Module Pilot
Apollo | 17 | 34 | Lunar Module Pilot
Skylab | 2 | 12 | Commander
Skylab | 2 | 41 | Pilot
Skylab | 2 | 25 | Scientist
Skylab | 3 | 4 | Commander
Skylab | 3 | 26 | Pilot
Skylab | 3 | 18 | Scientist
Skylab | 4 | 8 | Commander
Skylab | 4 | 31 | Pilot
Skylab | 4 | 19 | Scientist
Apollo-Soyuz | 1 | 39 | Commander
Apollo-Soyuz | 1 | 6 | Command Module Pilot
Apollo-Soyuz | 1 | 38 | Docking Module Pilot
Shuttle | STS-1 | 44 | Commander
Shuttle | STS-1 | 119 | Pilot
Shuttle | STS-2 | 138 | Commander
Shuttle | STS-2 | 408 | Pilot
Shuttle | STS-3 | 25 | Commander
Shuttle | STS-3 | 156 | Pilot
Shuttle | STS-4 | 28 | Commander
Shuttle | STS-4 | 191 | Pilot
Shuttle | STS-5 | 6 | Commander
Shuttle | STS-5 | 309 | Pilot
Shuttle | STS-5 | 53 | Mission Specialist
Shuttle | STS-5 | 245 | Mission Specialist
任何帮助都会很棒。
感谢。
答案 0 :(得分:0)
一种选择是对每个宇航员的记录使用条件聚合:
SELECT astrono
FROM yourTable
GROUP BY astrono
HAVING SUM(CASE WHEN projectname = 'Apollo' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN projectname = 'Shuttle' THEN 1 ELSE 0 END) > 0
答案 1 :(得分:0)
使用存在条件
从表t中选择astrono as where(从表中选择1 astrono = t.astrono,项目名称='Apollo')并存在(从表中选择1 astrono = t.astrono和projectname ='Shuttle');
答案 2 :(得分:0)
让我们一步一步来做。我们通过以下查询,我们从原始表中获取与function shortcode_image( $atts = array(), $content = null) {
$atts = $this->attributefix( $atts );
// [img="Google's Favicon"]http://www.google.com/favicon.ico[/img]
if ( isset($atts[0]) ) {
$alt = $atts[0]; //Google's Favicon
$img = $content; //http://www.google.com/favicon.ico
return '<img src="' . wp_get_attachment_url($img) . '" alt="' . do_shortcode( $alt ) . '" />';
}
// [img]http://www.google.com/favicon.ico[/img]
else {
$img = $content;
return '<img src="' . wp_get_attachment_url($img) . '" />';
}
}
和Apollo
项目有关的所有行。
Shuttle
然后,既然我们不关心具体的项目,那么让我们修改上面的查询,为每个宇航员返回项目项目:
SELECT * FROM mytable WHERE projectname IN ('Apollo', 'Shuttle')
最后,根据上述情况,我们只能保留参与至少两个项目的宇航员:
SELECT astrono, count(*) AS count
FROM mytable
WHERE projectname IN ('Apollo', 'Shuttle')
GROUP BY astrono
由于我们已经消除了涉及其他项目的所有行,并且如果我们假设宇航员和项目的组合最多可以出现一次,那么如果发现宇航员参与了基于上述计数的两个项目,那些两个项目必须是SELECT astrono
FROM mytable
WHERE projectname IN ('Apollo', 'Shuttle')
GROUP BY astrono
HAVING count(*)>=2;
和Apollo
。
如果您不喜欢上述假设,宇航员和项目的每个组合最多只能出现一次,您可以选择如下:
Shuttle