sql - SELECT COLUMN_NAMES is VALUE in Other TABLE (mysql) -


tbl 1

+--------+------+------+------+------+-----+ |   id   |  th  | room |    |  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 | +--------+------+------+------+------+-----+ 

tbl 2

+--------+------+------+ |   id   | rom  | clm  | +--------+------+------+ |      1 |   1  |    | |      2 |   1  |   b  | |      3 |   2  |    | |      4 |   3  |    | |      5 |   3  |   b  | |      6 |   4  |   c  | +--------+------+------+ 
  • if tbl_2.rom = 1 tbl_2.clm = a, want select column "a" tbl_1

  • if tbl_2.rom = 1 tbl_2.clm = b, want select column "b" tbl_1

  • if tbl_2.rom = 4 tbl_2.clm = c, want select column "c" tbl_1

how in query ? in advance

select a.room, case when b.clm='a' a.a when b.clm='b' a.b else a.c end  tbl_1 inner join tbl_2 b on a.room=b.room 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -