簡體   English   中英

使用 WHERE IN 時包含其他列

[英]include additional columns while using WHERE IN

我有一張看起來像這樣的桌子。

id        name
1         firstName
2         secondName
3         thirdName
4         fourthName

我想保留第二個表中“Country_name_EN”或“Country_Code”列中出現名稱的所有行,如下所示:

Country_name_EN  Country_Code        coordinates
firstName       EN                  124
random          secondName          1244
thirdName       DE                  689
FifthName       DE                  457

我想要一個看起來像這樣的結果表。 我還想包括在 table2 中找到名稱的所有行的坐標:

id        name                coordinates
1         firstName           124
2         secondName          1244
3         thirdName           689

此代碼適用於連接,但不確定如何將坐標包含在其中

select * 
  from `t1` as test
  where test.name in 
  (select test.name
      from `t2` as geonames 
      where geonames.Country_name_EN = test.name or geonames.Country_Code = test.name)

WHERE IN 子句總是可以重寫為連接,這在大型數據庫中通常比 IN 更胖,只有使用 JOIN 才能訪問兩個表中的列

所以你也是

SELECT `t1`.id, `t1`.name,`t2`.coordinates
from `t1` JOIN `t2` ON `t1`.name = `t2.Country_name_EN OR 

暫無
暫無

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

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