H
H
Habr2022-01-22 22:13:19
SQL
Habr, 2022-01-22 22:13:19

How to select 2 values ​​from 2 parameters and display them on 1 line?

Good day, how can I get 2 parameters in 1 request and display their values ​​on 1 line?

SELECT user.id, user.nickname, stat.param, stat.value FROM saved_users_stats AS stat
JOIN saved_users AS user ON userId = id
WHERE param = "[difficulty]chernobylhard [mode]PVE [stat]player_sessions_won" ORDER BY value DESC LIMIT 50

database structure and
61ec569e5e849311866546.png
query output
61ec5729036d9469199640.png
Is it possible to get the example structure:
id nickname won lost?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2022-01-22
@RozmarinUS

Making a PIVOT

select 
  user_id,
  sum(case when param = 'Won' then value else 0 end) 'Won',
  sum(case when param = 'Lose' then value else 0 end) 'Lose'
from user_stats
where param in ('Won', 'Lose')
group by user_id;

SQL PIVOT online

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question