Reputation:
How can I select data in the same query from two different databases into the same server? This is what I'm doing, but my query doesn't works:
$sqlquery = "SELECT * FROM database_2.table_2 WHERE database_1.table_1.data_1 LIKE database_2.table_2.data_2";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);
$i = 0;
if ($number < 1) {
print "DOH";
}else{
while ($number > $i) {
$content = mysql_result($result,$i,"database_2.table_2.data_3");
print "$content";
$i++;
}
}
Upvotes: 5
Views: 32853
Reputation: 710
The problem is not about different databases.
Your WHERE clause references the field database_1.table_1.data_1 which was not supplied in the FROM clause.
Didn't you mean something like
SELECT *
FROM database_2.table_2
JOIN database_1.table_1
ON (database_2.table_2.some_field = database_1.table_1.some_other_field)
WHERE database_1.table_1.data_1 LIKE database_2.table_2.data_2
?
Also,
echo mysql_error();
after your failed query - this will give you a clue about what's wrong.
Upvotes: 5
Reputation: 37233
try this
SELECT * FROM database_2.table_2 t2 INNER JOIN database_1.table_1 t1
ON t1.data_1 = t2.data_2
Upvotes: 0