簡體   English   中英

選擇表中ID等於另一個表中另一個ID的行

[英]Select rows in a table where id is equals to another id in another table

我想在我的getadvocacy表中選擇某一行,其中id是另一個表中的id,稱為Swimsuit。

泳衣桌

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

宣傳

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)

如果表的ID(主鍵)相同,則可以在ID上使用join

 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

在標准sql中,您必須執行以下操作:

select * 
from getadvocacy
where
    id in (select st.id from "swimsuit table" as st)

最簡單易懂:只需在兩行之間閱讀,您就會了解此代碼段的工作方式

SELECT getadvocacy.* , swimsuittable.*
FROM getadvocacy, swimsuittable
WHERE getadvocacy.id = swimsuittable.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM