I have this structure in my MySQL database:
+--------------+ +--------------+
| table_1 | | table_2 |
+----+---------+ +----+---------+
| id | name | | id | surname |
+----+---------+ +----+---------+
| 1 | tbl1_1 | | 1 | tbl2_1 |
| 2 | tbl1_2 | | 2 | tbl2_2 |
| 3 | tbl1_3 | | 3 | tbl2_3 |
+----+---------+ +----+---------+
I need a query that returns this:
+----+---------+---------+
| id | name | surname |
+----+---------+---------+
| 2 | tbl1_2 | tbl2_2 |
+----+---------+---------+
I searched in google, what I found is UNION which does this:
+----+---------+
| id | name |
+----+---------+
| 2 | tbl1_2 |
| 2 | tbl2_3 |
+----+---------+
which is not what I look for. I need they are combined next to each other, not on top of each other.
you need to use an JOIN, the type of join based on your requirement.
The INNER JOIN will select all rows from both tables as long as there is a match between the id columns we are matching on i.e the ids exist in both the tables