大多数在商店交易中销售产品

时间:2015-11-14 17:28:57

标签: mysql

我正在尝试构建一个查询,根据要获取的顶级产品数量和语言代码选择最畅销的产品。我不知道做得对,所以我很感谢你的帮助。

shop_transactions
id
1
2
3

shop_transaction_products_match
id | shop_transaction_id | product_id | units_bought
1    1                     1            4
2    2                     2            1
3    3                     2            2


products_translations
id | product_id | language_code | name | seo_name
1    1            es              Hola   hola
2    1            en              Hey u  hey-u
3    2            es              Adiós  adios
4    2            en              Bye u  bye-u

products
id | category_id
1    1
2    2

product_categories
id
1
2

product_categories_translations
id | category_id | language_code | name | seo_name
1    1             es              AA     aa
2    1             en              BB     bb
3    2             es              CC     cc
4    3             en              DD     dd

每行的信息是:product_translations的名称,product_translations的seo_name,product_categories_translations的名称,product_categories_translations的seo_name

按行销售的数量产品订购后代。

考虑到示例信息,如果要获取的产品数量为2且语言为es,则结果为:

Hola,hola,AA,aa - >已售出4件此产品

Adiós,adios,CC,cc - >已售出3件此产品

谢谢!

编辑:

到目前为止我尝试过的代码......我遗漏了一些我不知道该怎么做的事情:

SELECT pt.name, pt.seo_name, pct.name as name_category, pct.seo_name as seo_name_category
                                  From product_translations pt, products p, product_categories pc, product_categories_translations pct
                                  where p.id in (Select product_id from shop_transaction_product_match where shop_transaction_id in
                                  (Select id from shop_transactions)) AND pt.language_code = :language_code AND pct.language_code = :language_code
                                  AND p.category_id = pct.category_id

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DISTINCT
 pt.name, pt.seo_name, pct.name, pct.seo_name,
 SUM(stpm.units_bought) units_sold
FROM products AS p
 LEFT JOIN product_categories AS pc ON p.category_id = pc.id
 LEFT JOIN products_translations AS pt ON p.id = pt.product_id AND pt.language_code = 'es'
 LEFT JOIN product_categories_translations AS pct ON pc.id = pct.category_id AND pct.language_code = 'es'
 LEFT JOIN shop_transaction_products_match AS stpm ON p.id = stpm.product_id
-- Where p.id = 1
GROUP BY p.id
ORDER BY units_sold DESC
limit 2

答案 1 :(得分:0)

最好不要使用IN,而是使用INNER JOIN

SELECT products_translations.name,
           products_translations.seo_name,
           product_categories_translations.name,
           product_categories_translations.seo_name,
           SUM(shop_transaction_products_match.units_bought) AS Product_count
    FROM
         shop_transactions 
    INNER JOIN 
         shop_transaction_products_match
    ON shop_transactions.id=shop_transaction_products_match.shop_transaction_id
    INNER JOIN 
         products_translations 
    ON products_translations.product_id = shop_transaction_products_match.product_id
    INNER JOIN 
         products 
    ON products.id = shop_transaction_products_match.product_id
    INNER JOIN
        product_categories 
    ON products.category_id = product_categories.id
    INNER JOIN
        product_categories_translations
    ON product_categories_translations.category_id = product_categories.id AND product_categories_translations.language_code = products_translations.language_code 
    WHERE products_translations.language_code= 'es' GROUP BY products.id
    Limit 2

希望这有帮助。