A
A
Artem00712017-10-05 17:34:13
MySQL
Artem0071, 2017-10-05 17:34:13

How to make one request?

There are 3 tables:
1 - block (main_block_id, data, type)
2 - block_one (id, main_block_id, another_data)
3 - block_two (id, main_block_id, third_data)
1 and 2, and 1 and 3 are related
Each block has its own subblock in block_one or in block_two
This block is defined in block.type
I.e. if block.type == 'one', then JOIN with block_one will be taken
Now it's 2 queries
First determine what type, and then
look for Is it possible to do it all in one query?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2017-10-05
@res2001

Can. The case statement is here to help.

D
Denis Holub, 2017-10-05
@denman1985

Or

select a.data, b.another_data
from block as a join block_one as b on (a.main_block_id=b.main_block_id)
where a.type = 'one'
union all
select a.data, b.third_data
from block as a join block_two as b on (a.main_block_id=b.main_block_id)
where a.type = 'two'

Or
select distinct a.data, 
(case when a.type='one' then b.another_data else c.third_data end) as some_data
from block as a left join block_one as b on (a.type = 'one' and a.main_block_id=b.main_block_id)
left join block_two as c on (a.type = 'two' and a.main_block_id=c.main_block_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question