php MYSQL - 从不同的数据库中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18476912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MYSQL - SELECT from different databases
提问by mkf
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++;
}
}
回答by mkf
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.
您的 WHERE 子句引用了 FROM 子句中未提供的字段 database_1.table_1.data_1。
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.
在您的查询失败后 - 这将为您提供有关问题的线索。
回答by Ben Miller
isn't numrows underscored?
numrows 不是下划线的吗?
$number = mysql_num_rows($result);
回答by echo_Me
try this
尝试这个
SELECT * FROM database_2.table_2 t2 INNER JOIN database_1.table_1 t1
ON t1.data_1 = t2.data_2