C
C
CreativeStory2020-12-30 21:33:28
SQL
CreativeStory, 2020-12-30 21:33:28

How to write the right query for one table?

There is a table with users:

id | from_id | to_id
-----------------
1  | 20      | 32
2  | 76      | 10


Input data - 2 variables, they can come in this form - ( 32, 20 ) or in this form - ( 20, 32 )

What query can be made to get the correct string at the output?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-12-30
@CreativeStory

well let's say this:

select *
from `table_name`
where (from_id = 32 and to_id = 20)
   or (from_id = 20 and to_id = 32)
limit 1;

Just keep in mind that if there are two entries in the table: 32.20 and 20.32, then it is not possible to determine which of them is correct based on the input data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question