简体   繁体   English

用PHP连接两个MySQL查询

[英]Joining two Mysql Queries with php

Basically I have 2 SQL queries from 2 different databases and I am trying to compare where they are equal and then join together the other information for that value. 基本上,我有来自2个不同数据库的2个SQL查询,我试图比较它们相等的地方,然后将其他信息合并为该值。 My first query contains an id and a product name, my second query contains a product name and components. 我的第一个查询包含一个ID和一个产品名称,我的第二个查询包含一个产品名称和组件。 So I'm trying to join them on the product name and then show the other two bits of information with them. 因此,我尝试将它们加入产品名称中,然后与他们一起显示其他两部分信息。 The db I selected is being used in the second query. 我选择的数据库正在第二个查询中使用。 Any idea what I should do? 知道我该怎么办吗? So far I have this, which only seems to show one result: 到目前为止,我有这个,似乎只显示了一个结果:

$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'];
 }  
  }
 }

Thanks for any help 谢谢你的帮助

If the two databases are located on the same instance of MySQL (~ same machine), then you can refer to a table in say db2 from (say) db1 by prefixing the table name with the database name. 如果两个数据库位于MySQL的同一实例(〜同一台机器)上,则可以通过在表名前加上数据库名来引用db1db2表(例如db1 )。

Eg: 例如:

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 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM