D
D
d1zz72021-03-23 21:30:07
MySQL
d1zz7, 2021-03-23 21:30:07

How to display two lines in one?

There is (for example) a table matches and match_opponents.
Match_opponents has match_id and opponent_id (for each match there are two opponents, i.e. for match = 123 there will be two lines with match id and opponent id).

Select * 
from matches m
inner join match_opponents op on op.match_id = m.match_id
where m.status like "started";

This outputs two lines for each match, and I need one match in one line
like this:
match_id | opponent1 | opponent2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-03-23
@d1zz7

Use grouping

select
  m.match_id,
  min(opponent_id) as op1, max(opponent_id) as op2
from match_opponents op
inner join matches m on op.match_id = m.match_id
where m.status = "started"
group by m.match_id;

SQL fiddfe

T
ThunderCat, 2021-03-23
@ThunderCat

distinct + concat, though there will be only 2 fields

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question