Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't join the resullts

I want to find out all the employees who have the same number of children as the employees of department 111 and who were born between the years 1968 and 1969. So if in the department I have employees who have 1 and 2 children, the result of the query should be all the employees born on that date and who have 1 and 2 children

With this query i find out employees born in those years

select * 
from temple 
where fecna between '1968-01-01' and '1969-12-31';

Result of the query

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And here I find out all types of number of children in department 111

select distinct numhi 
from temple 
where numde = 111;

Result of the query

The result should be something like this

Expected result

I tried to join both select but returns wrong data

>Solution :

You could use the second query in a join condition like:

select t1.*, 
from temple t1 
inner join (
             select distinct numhi 
             from temple 
             where numde = 111
           ) as t2 on t1.numhi=t2.numhi
where t1.fecna between '1968-01-01' and '1969-12-31';

Please read MySQL JOIN and subquery

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading