2

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:

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.

Soulss
  • 171
  • 1
  • 12

1 Answers1

1

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
Skully
  • 2,882
  • 3
  • 20
  • 31
  • Using `LIMIT` without `ORDER BY` is generally a not well defined thing. – Tim Biegeleisen Dec 19 '21 at 04:53
  • Usually I use ```LIMIT 1``` to cap my result to one row if by any reason there's 2 users with the same email. I coded in PHP so that it checks if that user already exists and prevents that from occuring, but still is a safe measure is it not? – Soulss Dec 19 '21 at 04:59