Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MYSQL-在一个查询中组合2个表,但有2个WHERE/in子句_Php_Mysql_Database - Fatal编程技术网

Php MYSQL-在一个查询中组合2个表,但有2个WHERE/in子句

Php MYSQL-在一个查询中组合2个表,但有2个WHERE/in子句,php,mysql,database,Php,Mysql,Database,我希望在两个表上执行一个MYSQL查询,并将结果作为一个对象/数组得到。我的问题是,查询需要匹配两个表中名称不同的一组ID。 假设我有一个URL表和一个products表,它们都有一个列,对应于我正在搜索的ID,但名称不同。。。在URL表中,它被称为resource_id,在products表中,它被称为productid URL表: id |资源| id | url 产品表: productid |标题|说明|缩略图 因此,在php中,我有一个id列表,用于检索合并的两个表,但仅当url.re

我希望在两个表上执行一个MYSQL查询,并将结果作为一个对象/数组得到。我的问题是,查询需要匹配两个表中名称不同的一组ID。 假设我有一个URL表和一个products表,它们都有一个列,对应于我正在搜索的ID,但名称不同。。。在URL表中,它被称为resource_id,在products表中,它被称为productid

URL表: id |资源| id | url

产品表: productid |标题|说明|缩略图

因此,在php中,我有一个id列表,用于检索合并的两个表,但仅当url.resource_id和products.productid等于这些id时

以下是我到目前为止的情况:

$ids = ["18552", "18554", "18555", "18556"];
$newIds = implode(",",$ids);

$q3 = " SELECT products.title as title, urls.url as url
       FROM urls, products
       WHERE urls.resource_id IN ($newIds)
       AND products.productid IN ($newIds)";
此时,它将基于$ids变量返回4个结果,总共返回4次-16个结果。我认为这和查询中$newIds中的url.resource\u id和$newIds中的products.productid部分有关,因为4*4=16,但我可能错了

大概是这样的:

{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },

{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },

{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },

{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },
不知道如何在没有重复的情况下实现这一点。。。有什么建议吗

此外,该数据库来自一个购物车系统,因此我试图尽可能简化示例,而不会在我的问题/示例中失去清晰性


提前谢谢你

请尝试此查询。它显示了两个表中存在的所有入口以及$tdis中的a

$ids = ["18552", "18554", "18555", "18556"];
$newIds = implode(",",$ids);

$q3 = " SELECT p.title as title, u.url as url
       FROM urls u INNER JOIN products p ON u. resource_id  = p.productid 
       WHERE u.resource_id IN ($newIds);";

URL和产品中是否有一些公共字段,或者它们完全不相关?URL表中的资源id与产品表中的产品id相关。因此,在数组中的第一个ID中,URL中的资源ID将是18552,products表中的productid将是18552。