簡體   English   中英

如何根據多條記錄使用MySQL選擇多列?

[英]How to SELECT multiple columns with MySQL based on multiple records?

我現在有2張桌子,它們看起來如下:

points表如下所示:

id  country     points
1   4           1
2   7           5
3   8           4

sample表如下所示:

id   iso        pts
1    UK         100
2    US         300
3    AU         700
4    BO         1200
5    BA         1500
6    BR         2000
7    HR         5000
8    TD         10000
9    CA         15000

我想基本上是從選擇的所有數據points表,其中points.countrypoints.points對應sample.isosample.pts

所以我想要達到的結果是:

id  country     points
1   BO          100
2   HR          1500
3   TD          1200

這實際上是否可以實現? 如果有,怎么樣?

您必須兩次加入sample表才能獲得您所追求的信息( SQL FIDDLE ):

SELECT p.id, s1.iso AS country, s2.pts AS points
FROM points p
INNER JOIN sample s1 ON p.country = s1.id
INNER JOIN sample s2 ON p.points = s2.id  

在左連接或任何連接的幫助下,您可以執行此操作

select t1.id,t1.country,t1.points from table1 as t1 left join table2 as t2 on t1.id=t2.country

這聽起來像你正在尋找的。 與它的內部連接pointssample匹配points.countrysample.idpoints.pointssample.pts

你提供的數據沒有顯示任何points.points匹配sample.pts但我認為這是你正在拍攝的。 如果沒有,請在您的問題中澄清。

select p.id, s.iso, s.pts
  from points p, sample s
 where p.country = s.id
   and p.points = s.pts;

暫無
暫無

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

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