0

i have 2 tables in 2 different databases: database1.table1 that looks like:

id | word1
1    test1
2    test2
3    test3

and database2.table2 that looks like:

price | word2
10      test1
15      test2
30      test3

how can i compare word1 and word2 to see if the keys from word2 are found in word1 in either a mysql join or php

any ideas? Thanks

1
  • Do you want to check if all "keys" from word1 are in word2 or get the intersection? Commented Aug 19, 2011 at 18:34

1 Answer 1

6
SELECT t1.id, t1.word1, t2.price
  FROM database1.table1 t1 
  JOIN database2.table2 t2 
    ON t1.word1 = t2.word2;
Sign up to request clarification or add additional context in comments.

6 Comments

Run the query and the result set will contain what you want. Do you know how to execute mysql queries in PHP?
yes, lets say $result1 = mysql_fetch_array(query from table1) and $result2 = mysql_fetch_array(query from table2). $result1 will have both id and word1 and $result2 will have both price and word2. how do i compare $result1['word1'] and $result2['word2'] and still get all results
Why don't you just use the query in my answer? Your $result array will contain id, word and price without any post-processing needed. You did ask for either a mysql or php solution...
They are aliases for the tables, so instead of writing database1.table1 all the time, you can simply call t1. Try to do var_dump(mysql_fetch_array($the_query_here)); to see what the data looks like.
but why you place them in the beginning: SELECT t1.id, t1.word1, t2.price, they were not beeing alised until after the FROM
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.