简体   繁体   English

MYSQL-连接或子查询问题

[英]MYSQL - Join or Subquery Issues

I am trying to get a query to return all the relevant records for products which feature in my featured projects table. 我正在尝试查询以返回我的特色项目表中特色产品的所有相关记录。 All is working for the simple MYSQL query however I want to get the Thumb Image for each product two which is stored in another table (product_image) but with a catch. 所有人都在为简单的MYSQL查询工作,但是我想获取存储在另一个表(product_image)中但有问题的每个产品两个的Thumb Image。 In this "product_image" table not every products (product_id) will have a record as it is only populated on upload of images and some products may have several records (images stored) I only want one record returned for each product ID. 在此“ product_image”表中,不是每个产品(product_id)都有一条记录,因为它仅在上载图像时填充,并且某些产品可能具有多个记录(存储的图像),我只希望为每个产品ID返回一个记录。 So a product record in the products table has a 0 to many relationship with the product_image table. 因此,products表中的产品记录与product_image表之间存在0到许多的关系。

I have a working basic SQL statement for the basics without returning the image, I can code a query that only returns the products who have records in the product image table but I need the query to return all the rows in the outer query and then 1 thumb_img that matches the product id from the image table and if there is no images it can return a Null value. 我有一个可以正常工作的基本SQL语句,而没有返回图像,我可以编写一个查询,该查询仅返回在产品图像表中具有记录的产品,但是我需要查询返回外部查询中的所有行,然后返回1 thumb_img与图像表中的产品ID相匹配,如果没有图像,则可以返回Null值。

Is this possible? 这可能吗? Below is my latest attempt but this just returns the matching thumb for the first product_ID and duplicates this for the other 3(in this case due to limit at end) records. 以下是我最近的尝试,但这仅返回第一个product_ID的匹配缩略图,并与其他3个(在此情况下由于末尾的限制)记录重复。 I need the matching thumb for each record rather then the matching one for the first record repeated between the rest. 我需要为每条记录匹配的拇指,而不是在其余部分之间重复的第一条记录的匹配拇指。 And if there is no records for that product ID a null returned. 如果没有该产品ID的记录,则返回null。

SELECT * FROM `products`, `featured_products`, `shop`, (SELECT thumbSrc
          FROM product_image
         WHERE products.productId = product_image.productID
        ORDER BY product_image.position ASC
         LIMIT 1) image
where shop.shopId = products.shopId AND featured_products.productId = products.productId AND visible = '1' LIMIT 4

Thanks in advance for any help / feedback. 在此先感谢您的帮助/反馈。 Is it possible to do this as I want to keep my database as normalized as possible rather then result in storing a thumb field in the products table too. 是否可以执行此操作,因为我想使我的数据库尽可能保持规范化,而不是将结果字段也存储在products表中。

Untested, and there may be a better (performance) approach to the subselect... on PI I'm doing. 未经测试,在PI上对子选择可能有更好的(性能)方法。

Effectively what this is tying to do is return all products, the related feature products, and shops. 实际上,这样做的目的是退还所有产品,相关功能产品和商店。 Then return only those images which have a matching product; 然后仅返回具有匹配乘积的图像; but only if the image is the product image with the lowest position. 但仅当图像是位置最低的产品图像时。 This way the product, feature_product and shops don't get excluded if an image is missing (thus the nature of a LEFT [outer] join) 这样,如果缺少图像,就不会排除产品,feature_product和商店(因此,LEFT [外部]连接的性质)

SELECT *
FROM PRODUCTS P
INNER JOIN Featured_Products FP
 on P.ProductID = FP.ProductID
INNER JOIN SHOP S
 P.ShoPID = S.ShopID
LEFT JOIN (Select thmbsrc, productID, position, min(Position) as MinPosition
           FROM Product_Image PI
           group by thmbsrc, productID, Position
           having MinPosition = position) PI
 on PI.ProductID = P.ProductID

Performance may be improved if we first get a list of all the productIDs with the lowest position and then join it back to the product_image to get imgsrc. 如果我们首先获得位置最低的所有productID的列表,然后将其重新加入product_image以获得imgsrc,则可能会提高性能。 Something like ( Select min(position) minPos, productID from product_Image ) may allow the SQL engine to look at each record once, and not for every value in a having clause (if position is indexed then this would even be faster). 类似于( Select min(position) minPos, productID from product_Image )之类的内容,SQL引擎可以查看每条记录一次,而不是对hading子句中的每个值进行查看(如果对位置进行索引,则速度会更快)。

So... 所以...

SELECT *
FROM PRODUCTS P
INNER JOIN Featured_Products FP
 on P.ProductID = FP.ProductID
INNER JOIN SHOP S
 P.ShoPID = S.ShopID
LEFT JOIN (Select imgsrc FROM Product_Image PI
           INNER JOIN (Select min(position) minPos, productID 
                       from product_Image
                       GROUP BY ProductID) PI2
            on PI.ProductID = PI2.ProductID
           and PI.position = PI2.PminPos)
 on PI.ProductID = P.ProductID

May perform better depending on table statistics and available indexes. 根据表统计信息和可用索引,性能可能会更好。

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

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