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

Time:12-19

So I have the following row of my table1 table:

email name last_name activated
[email protected] john connor 1

Which I can get using:

select email, name, last_name, activated from table1
where email = [email protected]
limit 1;

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

status end_period qtd
1 1828232 20

Which I can get by using:

select status, end_period, qtd from table2
where email = [email protected]
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
[email protected] 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.

CodePudding user response:

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 = '[email protected]';

This results in your expected output:

email name last_name activated status end_period qtd
[email protected] john connor 1 1 1828232 20
  • Related