SELECT 来自 2 个条件不同的表
SELECT from 2 tables with different conditions
我试了大约 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
请注意,这只是一个直观地展示我的问题的例子。我的table的设置就不用说了吧
我想知道的:
首先,我需要来自 table people
的所有条目,其中分数小于 24(很容易到这里)。
下一步是只显示 people.name 在 table runs
中没有 s
的结果(我的实际问题)。
输出应该是:
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"
我知道这很糟糕 :)。但它用两行描述了我需要的东西。
如有任何帮助,我将不胜感激。
谢谢!
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)
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
我试了大约 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
请注意,这只是一个直观地展示我的问题的例子。我的table的设置就不用说了吧
我想知道的:
首先,我需要来自 table people
的所有条目,其中分数小于 24(很容易到这里)。
下一步是只显示 people.name 在 table runs
中没有 s
的结果(我的实际问题)。
输出应该是:
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"
我知道这很糟糕 :)。但它用两行描述了我需要的东西。
如有任何帮助,我将不胜感激。
谢谢!
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)
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