[Mysql] Meerdere tabellen lookup

Pagina: 1
Acties:

  • XiniX88
  • Registratie: December 2006
  • Laatst online: 12-11 08:38
Probleem
Aan een product hangen meerdere productopties met als voorbeeld:
code:
1
2
3
4
5
6
7
8
9
Naam product: AMD 3000+
+----------------------------------------------+
|Productfield_name      |string_text           |
+-----------------------+----------------------+
|Snelheid               |3000mhz               |
|Family                 |Barcelona             |
|Cores                  |1                     |
|Kleur                  |Zwart                 |
+----------------------------------------------+

waarbij Kleur als voorbeeld wordt gegeven aangezien deze kan verschillen in meerdere talen (dit heb ik weggelaten). Nu wil ik echter filters gaan opzetten, hierbij is het de bedoeling dat de klant het volgende kan:

code:
1
2
Cores: [ 1          | V ]
Kleur: [ Zwart      | V ]


en vervolgens worden de producten hierop gesorteerd. De boxes zelf vullen lukt wel, echter de producten ophalen waarbij meer dan 1 veld is gegeven (dus en Cores word op gefilterd en kleur) tast ik in het duister hoe dit op te lossen

Tabellen
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Tabel: products
+--------------------------+
|product_id |product_name  |
+-----------+--------------+
|1          |AMD 3000+     |
|2          |AMD x2 3000+  |
|3          |AMD x2 4000+  |
+--------------------------+

Tabel: productfield_name
+----------------------------------------------+
|Productfield_id        |productfield_name     |
+-----------------------+----------------------+
|1                      |Snelheid              | 
|2                      |Family                |    
|3                      |Cores                 |
|4                      |Kleur                 |     
+----------------------------------------------+

Tabel: productproperties
+------------------------------------------------------------------------+
|productproperty_id |product_id  |productfield_id |productproperty_name  |
+-------------------+------------+----------------+----------------------+
|1                  |1           |1               |3000mhz               |
|2                  |1           |2               |Barcelona             |
|3                  |1           |3               |1                     |
|4                  |1           |4               |Zwart                 |
|5                  |2           |1               |3000mhz               |
|6                  |2           |2               |Barcelona             |
|7                  |2           |3               |2                     |
|8                  |2           |4               |Zwart                 |
|9                  |3           |1               |4000mhz               |
|10                 |3           |2               |Barcelona             |
|11                 |3           |3               |2                     |
|12                 |3           |4               |Zwart                 |
+------------------------------------------------------------------------+


Resultaat
Ik krijg al netjes een array met:

filter[productfield_id] = productpropertyname

dus:
filter[1] = 3000mhz
filter[2] = Barcelona

Nu wil ik dus dat hij met product ID 1 en 2 komt, (3 = 4000mhz processor, dus moet niet worden weergegeven).

Tip
Je hoeft eigenlijk alleen met de tabel productproperties te werken, ik hoef alleen de product_id's terug te krijgen, de rest join ik er wel aan vast.

Iemand die mij in 1 query dit kan laten oplossen

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 14-11 08:44

thomaske

» » » » » »

Je moet per gekozen property een join definiëren. Iets in de trant van: (niet getest)

code:
1
2
3
4
5
6
7
8
9
10
11
SELECT p.* 
FROM products p
INNER JOIN productproperties pp ON 
    pp.product_id = p.product_id AND 
    pp.productfield_id = 1 AND 
    pp.productproperty_name = '3000mhz'

INNER JOIN productproperties pp ON 
    pp.product_id = p.product_id AND 
    pp.productfield_id = 2 AND 
    pp.productproperty_name = 'Barcelona'

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


  • XiniX88
  • Registratie: December 2006
  • Laatst online: 12-11 08:38
Ja daaag... zit ik gewoon al uren met de verkeerde tabel te kloten.... voelt zich heel dom (A) (Ik heb hier het voorbeeld al gemakkelijk gehouden, in het echt heb ik nog een filter tabel en een string (waarin vertalingen staan) tabel)

Eindresultaat
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SELECT p.product_id, SUM(ii.invoiceitem_quantity) as quantity
            FROM per_products AS p
            LEFT JOIN per_prices AS pr ON (p.product_id=pr.product_id)
            LEFT JOIN per_manufacturers AS m ON(p.manufacturer_id=m.manufacturer_id)
            LEFT JOIN per_invoiceitems AS ii ON (p.product_id=ii.product_id)
                    LEFT JOIN per_productproperties AS pp5 ON (pp5.product_id=p.product_id AND pp5.productfield_id = 5)
                    INNER JOIN per_strings AS f5 ON (pp5.productproperty_value = f5.string_id AND f5.string_text='Windows Mobile 6 Professional')
            
                    LEFT JOIN per_productproperties AS pp2 ON (pp2.product_id=p.product_id AND pp2.productfield_id = 2)
                    INNER JOIN per_strings AS f2 ON (pp2.productproperty_value = f2.string_id AND f2.string_text='microSD (micro Secure Digital)')
            
                    LEFT JOIN per_productproperties AS pp3 ON (pp3.product_id=p.product_id AND pp3.productfield_id = 3)
                    INNER JOIN per_strings AS f3 ON (pp3.productproperty_value = f3.string_id AND f3.string_text='Nee')
             WHERE p.product_enabled='true'
                AND p.category_id IN (3,5,6,4,1)
                
                 AND p.product_parent=0
                
                
            GROUP BY p.product_id
            ORDER BY   p.product_id DESC
            LIMIT 0, 9


TNX TNX TNX!

[ Voor 33% gewijzigd door XiniX88 op 11-06-2008 12:24 ]