包含儿童产品的十大产品清单

时间:2016-04-09 23:54:36

标签: mysql

对于任何SQL头来说,这都很容易,但出于某种原因暗示着我。

Table product: product_id, parent_id, etc
Table carts_ordered: product_id, quantity, etc

产品儿童parent_id设置为其父级product_id

这大致是我现在正在做的事情:

SELECT c.product_id, p.parent_id, SUM(c.quantity) AS su 
FROM carts_ordered c 
LEFT JOIN product p ON (c.product_id=p.product_id) 
WHERE p.product_id IS NOT NULL 
GROUP BY c.product_id
ORDER BY su DESC LIMIT 0,10

但当然,我经常会在前十名中获得一堆儿童用品,例如:

enter image description here

我想合并所有产品儿童的总和,以获得前十名父产品列表(前十名中没有儿童产品)。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

正确使用left joininner join可以替换为quantity

从问题描述中,最好将父母的SELECT p.parent_id + IF(p.parent_id = 0, p.product_id, 0) as ParentID, SUM(c.quantity) AS su FROM carts_ordered c INNER JOIN product p ON (c.product_id=p.product_id) GROUP BY ParentID ORDER BY su DESC LIMIT 0,10; 与他们所有的孩子相加,而不仅仅是孩子的总和。

For Dynamic Height:
 1. Use auto layout when creating your table view cells.
 2. Set the table view rowHeight to equal UITableViewAutomaticDimension.
 3. Set the estimatedRowHeight or implement the height estimation delegate method.

    Pro Tip: the trick to getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.
    Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.
    The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.
    If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.


For Detail, please refer to original source of above text is https://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

答案 1 :(得分:0)

为什么不按parent_id分组?

SELECT c.product_id, p.parent_id, SUM(c.quantity) AS su 
FROM carts_ordered c 
    LEFT JOIN product p ON (c.product_id=p.product_id) 
WHERE p.parent_id IS NOT NULL 
GROUP BY p.parent_id
ORDER BY su DESC LIMIT 0,10

有用吗?我没有安装mysql,无法测试。