tags:

views:

46

answers:

1

How do i 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 a matching name in name.table1) { give me the address,phone,account }

+1  A: 

I suppose you want to use JOINs

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

There are different types of JOINs, which one to use will depend on what you need. Here's a question on visualizing the different types of JOINs.

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
NullUserException
this doesn't works, it always takes id=1
Bowr
I am not sure if I understand your question. You can do a JOIN on any field, it doesn't have to be id.
NullUserException
DOESN'T WORKS:$result = mysql_query("SELECT * FROM table1 INNER JOIN table2 ON table1.name = table2.name2")because it always gets the address,phone,account of the last record id
Bowr
@Bowr See my edited response
NullUserException
OK i will make it work with your help, thank you very much
Bowr
@Bow Do I get an accept mark? =D
NullUserException