2016-07-24 89 views
0

我想在我的getadvocacy表中选择某个行,其中id是另一个名为swimsuit的表中的id。选择表中的行,其中id等于另一个表中的另一个id

泳装表

id   |   name   |  average 
1   |   Abc   |  90 
3   |   Def   |  99 

getadvocacy

id   |  all_scores  |  average_score 
1   |  70,70,70  |  70 
2   |  70,70,70  |  70 
3   |  70,70,70  |  70 

现在,我想从getadvocacy选择,但只有1和3,因为这是泳装的数据。

预期输出

id   |  all_scores  |  average_score 
1   |  70,70,70  |  70 
3   |  70,70,70  |  70 

我试过,但它有不同的输出。

select getadvocacy.id, all_scores, average_score from getadvocacy WHERE getadvocacy.id IN (select id from swimsuit) 
+1

你是什么意思*没有工作*:你有错误吗?你有不同的结果吗?你能澄清一下吗? – trincot

+0

获得不同的结果。抱歉。我会编辑它。 –

+1

我把你的问题放在小提琴里,它*会给你想要的输出:http://sqlfiddle.com/#!9/63069/1。你没有告诉我们什么? – trincot

回答

1

如果ID(主键)是一样的表,那么你可以使用加入的ID

select * from table1 
     JOIN table2 
      on table1.id = table2.id 

使用此

select * from swimsuit JOIN getadvocacy ON swimsuit.id= getadvocacy.id; 

查询结果是

1 abc 90 1 50,60,70 70 
    3 def 99 3 60,70,70 70 
+1

我认为这不是正确的方法,因为如果table2对于table1的某个id有更多的行,您将获得相应记录的更多副本,标准方式是使用“in”运算符。 –

+0

问题在于OP已经有了正确的代码。 @SeeMore,你能评论一下吗? – trincot

0
标准SQL

你要做的:

select * 
from getadvocacy 
where 
    id in (select st.id from "swimsuit table" as st) 
相关问题