Compare 2 fields in 2 tables and extract data

How to compare 2 fields in 2 tables, find a match and use it? Example:

table1: data1 (id_data1, name, address, phone, account) table2: data2 (id_data2, name2)

now in php: if (name2.table2 has the same name in name.table1) {give me the address, phone, account}

+3
source share
1 answer

Suppose you want to use JOINs

SELECT data FROM table1 INNER JOIN table2 ON table1.id=table2.id

There are different types of JOINs that will be used depending on what you need. Here is a question about visualizing various types of connections.

EDIT: I believe you are looking for something like this:

SELECT table1.name, phone, other_info_here 
FROM table1 AS t1 RIGHT JOIN table2 AS t2 ON t1.name=t2.name 
WHERE t1.name IS NOT NULL
+1
source

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


All Articles