简体   繁体   中英

How can we get a result from the following tables for a some key values , when the values can change based on user's upload

P1 (Master table) Cols:Teamname TeamID

              Data: ABC      1 
                    DEF      2 

P2 (Fieldmapping) Cols:FieldDesc Fieldname TeamID:

              Data:Goals     Text1     1,
                   Player    Text2     1,
                   Shots     Text1     2,
                   Batsman   Text2     2,
                   Runs      Text3     2,

P3 (Values) Cols:TeamID text1 text2 text3 text4 text5 text6 text7 text8 text9 text10

              Data:  1      3   John
                     1      2   Mark
                     1      1   Stu
                     2      10  Bart   14
                     2      3   Mary   6
                     2      50  Ba     100

Expected Output: If the user selects the ABC then the output should be

O/p: Col: Team Goal Player

 Data: ABC  3   John
       ABC  2   Mark
       ABC  1   Stu

Expected Output: If the user selects the DEF then the output should be

 O/p: Col: Team Shots Batsman Runs

 Data: DEF  10   Bart    14
       DEF  3    Mary    6
       DEF  50   Ba     100

How can i write this in SQL?

You need to learn how to use join in sql queries. Here you can join table p1 and table p3 on teamId to get the desired results.

select
  teamname,
  shots,
  batsman,
  coalesce(runs, 0) as runs
from p1
join p3
on p1.teamId = p3.teamId
where teamname = 'DEF'
order by
  teamname

Output:

| teamname | shots | batsman | runs |
| -------- | ----- | ------- | ---- |
| DEF      | 10    | Bart    | 14   |
| DEF      | 3     | Mary    | 6    |
| DEF      | 50    | Ba      | 100  |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM