Saturday 15 September 2012

mysql - Efficient way to count combinations of records with SQL? -


Looking at the order table and a related table of items in those orders, I need to find the order number with each SKU Possible combinations I'm doing in Magento, but this is actually just a SQL question, because I'm working directly at the database level.

Here is the relevant section of each table:

sales_flat_order

  | Unit_id | Int (10) unsigned. - Primary key   

sales_flat_order_item

  | Order_id | Int (10) unsigned. - The foreign key for the order of the unit_ID of the table. | Sku | Varchar (255) |   

I am dragging the data that I want to use in a subcategory and MySQL GROUP_CONCAT function, but I would like to know if there is any way To do this, it works without more efficient and / or proprietary work. Here I am currently:

  select skulist, COUNT (skulist) from (select group_concat by sku (Different sku orders) orders_entit_id = items.order_id group_sw_type_with_email_with_email_groups_with_email_groups_with_email_group_site_image_groups Skulists by group skulists;   

Is there any better way of doing this? The text "itemprop =" text ">

This is, in fact, the best way to do this with a small warning is to enter your query < Code> can be more effective by joining :

  select skulist, from COUNT (skulist) (as skulist by group_concat sku (separate sku command) Selected by Sales_flat_order_item item group Order_ID) skulists by group skulist;   

It is difficult to not use database-specific functions, in most databases there is a way to group_concat (specific ...) . As far as I can tell, some two do it the same way (okay, in fact it is not true because postgres use the same syntax in the derived database but quite near).

In standard SQL, you can easily find all the permutations of n objects for any n However, Can not think in a way to do this for combinations.

No comments:

Post a Comment