使用 php 连接两个 Mysql 查询


Joining two Mysql Queries with php

>基本上,我有来自 2 个不同数据库的 2 个 SQL 查询,我正在尝试比较它们相等的位置,然后将该值的其他信息连接在一起。我的第一个查询包含 id 和产品名称,第二个查询包含产品名称和组件。 所以我正在尝试将他们加入产品名称,然后向他们展示其他两部分信息。我选择的数据库正在第二个查询中使用。知道我应该怎么做吗?到目前为止,我有这个,它似乎只显示一个结果:

$catid = mysql_query("Select a.entry_id, b.cat_name from blog.exp_category_posts a inner join blog.exp_categories b on a.cat_id=b.cat_id where b.Group_ID = 3");
$results = mysql_query("Select a.name, c.product from wr_scientific a inner join wr_scientific_products b on a.id=b.scienc_id Join xcart_products c on b.prod_id=c.productid LIMIT 1000");
while($row1 = mysql_fetch_array($catid)){
$row2= explode("™", $row1['cat_name']);
$row3= explode("®", $row2[0]);
while($row = mysql_fetch_array($results)){
$rowpro = explode("™", $row['product']);
$rowprod = explode("®", $rowpro[0]);
if($rowprod[0] == $row3[0]){
echo $rowprod[0].$row['name'].$row1['entry_id'];
 }  
  }
 }

感谢您的任何帮助

如果两个数据库位于MySQL的同一个实例(~同一台机器)上,那么您可以通过在表名前面加上数据库名称来引用db2 db1中的表。

例如:

USE db1 ;
SELECT
    db1.table_in_db1.id,  -- you can specify the database name here, but
    table_in_db2.id,      -- there is no ambiguity, the database name is optional
    field_in_table_in_db2 -- the same way the table name is optionnal when there is no ambiguity
FROM
    db1.table_in_db1      -- database name is optionnal here, current database is assumed
JOIN
    db2.table_in_db2
ON ...