我现在有2张桌子,它们看起来如下:
该points表如下所示:
id country points
1 4 1
2 7 5
3 8 4
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
我基本上想要的是从points表中选择所有数据,points.country并且points.points对应于sample.iso和sample.pts.
所以我想要达到的结果是:
id country points
1 BO 100
2 HR 1500
3 TD 1200
Run Code Online (Sandbox Code Playgroud)
这实际上是否可以实现?如果有,怎么样?
您必须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
Run Code Online (Sandbox Code Playgroud)