2

我已经尝试了大约 2 个小时,但没有成功。这是我需要做的一个例子:

people

nameA           score
---------------------
someone1        24
someone2        24
someone3        24
someone4        23
someone5        21
someone6        24
someone7        19
someone8        20
someone9        24
someone10       24

runs

nameB           add
---------------------
someone1        s
someone2
someone2
someone4        s
someone5
someone4
someone7        s
someone8        s
someone7
someone7        s

请注意,这只是直观显示我的问题的示例。无需谈论我的桌子的设置。

我想知道的:
首先,我需要people分数小于 24 的表中的所有条目(简单到这里)。

下一步是仅显示 people.names在表中没有的结果runs(我的实际问题)。

输出应该是:

name            score
---------------------
someone2        22      /*  no "s"-entry in runs        */
someone5        21      /*  also no entries             */

不幸的是,我对连接不太熟悉。这是我最后一次尝试:

SELECT nameA,score FROM runs,people WHERE people.nameA = runs.nameB
AND people.score < 24
AND runs.add != "s"
GROUP BY people.nameA

在伪代码中:

foreach Person in (SELECT nameA as Person,score FROM people WHERE score < 24)
SHOW name.B = Person WHERE add != "s"

我知道这是狗屎:)。但它用两行描述了我需要的东西。

我会很感激这里的任何帮助。
谢谢!

4

2 回答 2

2
select * from people where score<24 and 
name in (select name from runs where add!=s) and
name not in (select name from runs where add=s)
于 2015-02-17T10:03:11.097 回答
1
SELECT people.nameA,runs.score FROM runs,people WHERE people.nameA = runs.nameB
AND people.score < 24
AND people.nameA NOT IN (SELECT runs.nameB FROM RUNS WHERE runs.add = "s")
GROUP BY people.nameA
于 2015-02-17T10:07:45.427 回答