O
O
Optimus2015-12-14 11:07:50
MySQL
Optimus, 2015-12-14 11:07:50

How to make a join request?

I can’t enter the join in any way, and even there the left one, the right one is not clear, I need to make 2 requests

SELECT COUNT(*) FROM `table_1` WHERE `id`='7'
SELECT COUNT(*) FROM `table_2` WHERE `id`='7'

In this case, these 2 queries can be combined into one with join? If so, how?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nozzy, 2015-12-14
Pyan @marrk2

I can't figure out why to join them.
If you need one line:

SELECT COUNT(*) as t1_count, NULL FROM `table_1` WHERE `id`='7'
UNION 
SELECT NULL, COUNT(*) as t2_count FROM `table_2` WHERE `id`='7'

with join:
select 
COUNT(*) as t1_count,
t2.t2_count
FROM `table_1` t1
inner join 
(
SELECT id, COUNT(*) as t2_count FROM `table_2` WHERE `id`='7' 
) t2 on t2.id = t1.id
where t1.id = '7'

D
Dmitry Kovalsky, 2015-12-14
@dmitryKovalskiy

Without a table structure, there is nothing to help you. If you want a ready-made request, write entities and fields by which they can be glued, but you won’t be able to help you with such a spherical horse in a vacuum. Well, or deal with the union of sets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question