Frage

I'm a bit confused when trying to create a specific query with the following data tables:

**table 1 - referral_data:**

ID      attribution_name
------------------------
1       Category
2       Brand
3       Size
4       Color
5       Processor
6       OS
7       Screen Size
 .....

**table 2 - referral_values:**

ID     ref_data_id          attribution_value
---------------------------------------------
1      1                    Cell Phones
2      1                    Tablets
3      1                    Laptops
4      1                    Computers
5      1                    LCD Monitors
6      2                    Nokia
7      2                    Motorola
8      2                    Samsung
9      2                    Lenovo
10     2                    Philips
11     3                    10x10x11
12     3                    100x100x20
13     3                    10x200x200
14     3                    2x2x3
15     4                    Black
16     4                    Cyan
17     4                    Magenta
18     4                    White
19     4                    Blue
20     5                    ARM Cortex A11
21     5                    Snapdragon 11
22     5                    Intel I3 XXXXX
23     5                    Exynos XXXX
24     6                    Android 4.1
25     6                    Android 3.0
26     6                    Windows Phone 3
27     6                    Windows 8 Professional
28     7                    18.5"
29     7                    11.8"
30     7                    7.0"
31     7                    5.0"
32     7                    3.5" 
 ......

**table 3 - product_specs:**

ID   product_id    referral_data_id    referral_value_id
--------------------------------------------------------
1    1050          1                   1                 // <-- Product 1 - Category: Cell Phones
1    1050          2                   8                 // <-- Product 1 - Brand: Samsung
1    1050          4                   19                // <-- Product 1 - Color: Blue
1    1050          6                   24                // <-- Product 1 - Processor: Exynos XXXX
1    1050          7                   30                // <-- Product 1 - Screen Size: 7.0"
1    1068          1                   4                 // <-- Product 2 - Category: Computers
1    1068          2                   9                 // <-- Product 2 - Brand: Samsung
1    1068          6                   22                // <-- Product 2 - Processor: Intel Core I3
1    1068          7                   28                // <-- Product 2 - Screen Size: 18.5"
 ......

These tables consists in a "Product Catalog" that I'm planning to use in a e-commerce website.

This is intended to optimize "client side" search functions and organize internal product data information, turning the "content-administrators" tasks in a simplest and easiest environment as possible. (Letting them, for example, choosing an "already-entered" data values instead of re-entering an "already entered data", avoiding duplicated data or typo errors).

The "content administrators" will have options, according to that "table dynamics", to insert new attribution data (product characteristics) and or new attribution values to them (attribution values).

Info: the product code field named "product_id", is outside the tables relation, this is just used to create a link to attach informations to products that they belong to.

In general SQL Joins to get data over the tables, I'm Ok. But there some kind of informations that I need to get / manage, I'm getting nuts. I've spent A LOT of hours and I just have found a headache.

My question is about how to build, in a single query, to get commonly used referral data, based on a CATEGORY. (when the contents admin choose, for a example, "Cell Phones" in "Category" field, they will get a commonly used "data table information" about that Category, like Brand, Color, Screen Size, etc .... to just choose their category attribution ) and to create a similar query to highlight or order by the commonly used attribution values ( i.e. commonly used screen sizes ).

War es hilfreich?

Lösung

In a single query?

SELECT ps2.product_id,rv2.attribution_value,rd.attribution_name 
  FROM ((
      (select ps.* from product_specs ps JOIN referral_values rv 
        ON rv.ref_data_id=ps.referral_value_id 
        WHERE rv.attribution_value = 'Cell Phones'
      ) ps1 
    JOIN product_specs ps2 on ps1.product_id=ps2.product_id) 
  JOIN referral_values rv2 ON ps2.referral_value_id = rv2.id)
JOIN referral_data rd ON rd.id = rv2.ref_data_id;

The inner select is used to get the right product_id based on criteria 'Cell Phones'. The others are used to populate this value with all details. To do this the first JOIN is a self-JOIN of product_specs to get all data, the following two joins are used to get the string values of them.

By the way: The column product_specs.referral_data_id is redundant and can/should be removed

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top