2016-07-24 91 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) 
相關問題