How to select records from two different tables in one MySQL query?

I have the first names stored in one table, and the last names are stored in another. I know this is stupid, but I try different things as I am just starting with MySQL. In any case, is it possible to select the first name from one table and the last name from another in one query? And put the result inside a PHP variable?

+3
source share
3 answers

You should have what links the two tables together, which is common key. Something like Idthe following example:

Table 1

Id Fname
--------
1 Roger
2 Pete

Table 2

Id Lname
--------
1 Federer
2 Sampras

In this case, you can get the full name as:

SELECT Fname, Lname from T1,T2 where T1.Id = T2.Id;
+6
source

Use joins

SELECT firstName, lastName  
FROM Table1, Table2 
WHERE (Table1.id = Table2.id)
0
select table1.firstname, table2.lastname from table1, table2
    where table1.id = table2.id

. .

SELECT FROM , , MySQL .

0

Source: https://habr.com/ru/post/1738778/


All Articles