简体   繁体   English

搜索多个表的mysql

[英]Search through multiple tables mysql

I have a table Product and Classification and a join table Product_Classification. 我有一个产品表和分类表以及一个联接表Product_Classification。 I wrote a query to search through the tables. 我写了一个查询来搜索表。 One thing I notice is that If I have a product record (or Classification) that is not mapped to Classification the query will not return anything. 我注意到的一件事是,如果我有未映射到“分类”的产品记录(或“分类”),则查询将不会返回任何内容。 How can I change my query in a such a way that it does ALSO return a Product that is not mapped a Classification (and a Classification data that is not mapped to a product). 如何以这样的方式更改查询,即也返回未映射到分类的产品(以及未映射到产品的分类数据)。

$query = "Select *   from $dbname.Product P INNER JOIN Product_Classification PC ON P.ProductID = PC.ProductID INNER JOIN Classification C ON PC.ClassificationID = C.ClassificationID ";

EDIT: I do have a Where condition, which is an array of fields 编辑:我确实有一个条件,这是一个字段数组

You can use this query 您可以使用此查询

$query = "Select *   from $dbname.Product P LEFT JOIN Product_Classification PC ON P.ProductID = PC.ProductID LEFT JOIN Classification C ON PC.ClassificationID = C.ClassificationID ";

If you want to see more about joins, this link will help you https://stackoverflow.com/a/4715847/6098214 如果您想了解有关联接的更多信息,此链接将为您提供帮助https://stackoverflow.com/a/4715847/6098214

使用此查询

Select *   from $dbname.Product P INNER JOIN Product_Classification PC ON P.ProductID = PC.ProductID LEFT JOIN Classification C ON PC.ClassificationID = C.ClassificationID

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

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