简体   繁体   English

SQL内部联接多个表

[英]SQL Inner Join multiple tables

I have a question for my application, that uses MySQL. 我对使用MySQL的应用程序有疑问。 I have a table with products and another with the media from the products (with photos, videos, etc.) - that has a multiples itens from products. 我有一张桌子,上面放着产品,另一张桌子上有产品上的媒体(带有照片,视频等)-桌子上有多个商品。 That table has a FK with the ID of the product. 该表具有带有产品ID的FK。

I need to load a product with all the media (that is multiple registries). 我需要使用所有媒体(即多个注册表)加载产品。 Which is the best way to to this? 哪个是最好的方法?

I tried with the INNER JOIN but it receives the registries multiple times with the products duplicated (because I have multiples media itens). 我尝试了INNER JOIN,但是它多次复制产品而收到注册表(因为我有多个媒体itens)。 I think that is not the best way. 我认为这不是最好的方法。

Examples of the data and results you are getting would be helpful. 您获得的数据和结果示例将很有帮助。

But, based on what I can glean from your question so far, I think your problem will be solved by a simple addition of DISTINCT. 但是,根据到目前为止我可以从您的问题中收集到的信息,我认为只需添加DISTINCT即可解决您的问题。

eg Instead of SELECT ProductId, ProductName, MediaId, MediaName... do SELECT DISTINCT ProductId, ProductName, MediaId, MediaName 例如,而不是选择ProductId,ProductName,MediaId,MediaName ...选择SELECT DISTINCT ProductId,ProductName,MediaId,MediaName

Without the distinct, you get all linked rows between the two tables. 如果没有唯一性,您将获得两个表之间的所有链接行。 With the DISTINCT, you will get only unique combinations of the fields selected. 使用DISTINCT,您将仅获得所选字段的唯一组合

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

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