表1
+--------+------+------+------+------+-----+
| ID | th | room | A | B | C |
+--------+------+------+------+------+-----+
| 1 | 10 | 1 | 50 | 60 | 70 |
| 2 | 20 | 1 | 100 | 120 | 140 |
| 3 | 30 | 2 | 150 | 180 | 210 |
| 4 | 40 | 2 | 200 | 240 | 280 |
+--------+------+------+------+------+-----+
表2
+--------+------+------+
| ID | rom | CLM |
+--------+------+------+
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 2 | A |
| 4 | 3 | A |
| 5 | 3 | B |
| 6 | 4 | C |
+--------+------+------+
如果tbl_2.rom = 1 tbl_2.clm = A,那么我想从tbl_1中选择“ A”列
如果tbl_2.rom = 1 tbl_2.clm = B,那么我想从tbl_1中选择“ B”列
如果tbl_2.rom = 4 tbl_2.clm = C,那么我想从tbl_1中选择“ CLUMN”
在查询中该如何做?提前致谢
最佳答案
SELECT a.room, CASE WHEN b.clm='A' THEN a.A WHEN b.clm='B' THEN a.B ELSE a.C END
FROM tbl_1 a INNER JOIN tbl_2 b ON a.room=b.room