A
A
Alexander2019-10-15 11:42:02
MySQL
Alexander, 2019-10-15 11:42:02

Is it possible to conditionally execute an inner join?

There are tables such as:

tables

id | number | type | status
-----------------------------------
1  | 777777 | 1    | send
2  | 777778 | 2    | send
3  | 777779 | 3    | send

tab2:
id | number | comment
-----------------------------------
1  | 777777 | comment1
2  | 777778 | comment2
3  | 777779 | comment3


After executing the request, I want to get
spoiler

id | number | type | status | comment
------------------------------------------------------
1  | 777777 | 1    | send   | comment1
2  | 777778 | 2    | send  (нет поля comment) 
3  | 777779 | 3    | send  (нет поля comment)


I see a query like this:
Получить * из tab1 и если tab1.type = '1' inner join tab2 on tab1.number = tab2.number else не добавлять

But I can't figure out how case and if work. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-10-15
@AleksandrB

You can write directly in ON conditions

inner join tab2 on tab1.number = tab2.number and tab1.type = '1'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question