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

Combine 2 rows of different tables into one row in SQL?

So I have the following row of my table1 table:

email name last_name activated
ex@ex.com john connor 1

Which I can get using:

select email, name, last_name, activated from table1
where email = ex@ex.com
limit 1;

And then I have the other row of of my table2:

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

status end_period qtd
1 1828232 20

Which I can get by using:

select status, end_period, qtd from table2
where email = ex@ex.com
limit 1;

Is there any way I can get the results into one line?
Like this:

email name last_name activated status end_period qtd
ex@ex.com john connor 1 1 1828232 20

Ps: Both tables have an ’email’ column, which is what I use to link the the row of one to another.

>Solution :

What you are looking for is a table join, in this case an INNER JOIN between table1 and table2 where the email column matches.

SELECT table1.email, table1.name, table1.last_name, table1.activated, table2.status, table2.end_period, table2.qtd
FROM `table1` AS table1
INNER JOIN `table2` AS table2
ON table1.email = table2.email
WHERE table1.email = 'ex@ex.com';

This results in your expected output:

email name last_name activated status end_period qtd
ex@ex.com john connor 1 1 1828232 20
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