Lege resultaten bij foreign keys met null waarde

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • nowherebound123
  • Registratie: Mei 2009
  • Laatst online: 19-09 21:54
Heren,

Ik gebruik momenteel de volgende select query:

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
SELECT 
    p.productName,
    p.productDescription,
    p.productPhoto,
    p.productCategoryID,
    p.productColorID,
    p.productSizeID,
    p.productPrice,
    p.productStock,
    p.productAdded,
    c.categoryName,
    co.colorName,
    s.sizeName
                        
FROM 
    products AS p,
    categories AS c,
    colors AS co,
    sizes AS s
            
WHERE 
    p.productCategoryID = c.categoryID AND
    p.productColorID = co.colorID AND
    p.productSizeID = s.sizeID
                        
AND
    p.productID = ".mysql_real_escape_string($productID).";


Dat gaat goed zolang alle velden ingevuld zijn, maar zodra een van de keys (productColorID or productSizeID) een nullwaarde bevatten krijg ik alleen lege resultaten terug.

Iemand enig idee waar dat aan kan liggen?

Acties:
  • 0 Henk 'm!

  • whoami
  • Registratie: December 2000
  • Laatst online: 14:03
Je gebruikt nu INNER JOINS.
Gebruik OUTER JOIN's (zie ook de FAQ). Met een outer join kan je de records uit tabel x ophalen, ook als die geen gerelateerde records heeft in tabel Y.

(Is ook makkelijker als je de 'JOIN syntax' gebruikt, ipv te joinen in je where clause).

https://fgheysels.github.io/


Acties:
  • 0 Henk 'm!

  • nowherebound123
  • Registratie: Mei 2009
  • Laatst online: 19-09 21:54
Ugh.. zou je hiervan een voorbeeldje kunnen geven met mijn query? Ik ben de faq over de joins eens door aan het lezen maar weet niet precies hoe ik het moet implementeren.

Acties:
  • 0 Henk 'm!

  • PvdE
  • Registratie: April 2009
  • Laatst online: 07:33
Uit het hoofd, zou het zo iets moeten worden:

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
SELECT 
    p.productName,
    p.productDescription,
    p.productPhoto,
    p.productCategoryID,
    p.productColorID,
    p.productSizeID,
    p.productPrice,
    p.productStock,
    p.productAdded,
    c.categoryName,
    co.colorName,
    s.sizeName
                        
FROM  
    products as p
    LEFT JOIN categories AS c
    ON p.productCategoryID = c.categoryID

    LEFT JOIN colors AS co
    ON p.productColorID = co.colorID
       
    LEFT JOIN sizes AS s
    ON p.productSizeID = s.sizeID
            
WHERE 
    p.productID = ".mysql_real_escape_string($productID).";

Acties:
  • 0 Henk 'm!

  • Crazybest
  • Registratie: Januari 2002
  • Laatst online: 15-01-2023
ik weet niet of outer the default join is, anders moet je dus "left outer join" zetting ipv "left join" ;-)

Acties:
  • 0 Henk 'm!

  • nowherebound123
  • Registratie: Mei 2009
  • Laatst online: 19-09 21:54
hmm

de volgende query geeft geen errors, maar wel nog steeds lege results.

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
SELECT 
                        p.productName,
                        p.productDescription,
                        p.productPhoto,
                        p.productCategoryID,
                        p.productColorID,
                        p.productSizeID,
                        p.productPrice,
                        p.productStock,
                        p.productAdded,
                        c.categoryName,
                        co.colorName,
                        s.sizeName
                        
                    FROM 
                        products AS p
                        
                        LEFT OUTER JOIN categories AS c
                        ON p.productCategoryID = c.categoryID
                        
                        LEFT OUTER JOIN colors AS co
                        ON p.productColorID = co.colorID
                        
                        LEFT OUTER JOIN sizes AS s
                        ON p.productSizeID = s.sizeID
                    
                    WHERE 
                        p.productCategoryID = c.categoryID AND
                        p.productColorID = co.colorID AND
                        p.productSizeID = s.sizeID
                        
                    AND
                        p.productID = ".mysql_real_escape_string($productID).";

Acties:
  • 0 Henk 'm!

  • stappel_
  • Registratie: Augustus 2000
  • Laatst online: 14-09 12:59

Ubero: #2, Euler: #1, GOT: #1, Des: #1, Zeta: #1, Eon: #3, OGR-24: #3, OGR-25: #7,
LM: #7, AP: #5, DF: #19, D2OL: #37, SOB: #50, TSC: #63, RC5: #96


Acties:
  • 0 Henk 'm!

  • Amras
  • Registratie: Januari 2003
  • Laatst online: 14:15
Het gaat nu fout omdat je nogsteeds een INNER JOIN doet in je WHERE clause. Die statements in de WHERE clause zijn met gebruik van de JOIN syntax overbodig.

Acties:
  • 0 Henk 'm!

  • nowherebound123
  • Registratie: Mei 2009
  • Laatst online: 19-09 21:54
Amras schreef op woensdag 10 juni 2009 @ 12:30:
Het gaat nu fout omdat je nogsteeds een INNER JOIN doet in je WHERE clause. Die statements in de WHERE clause zijn met gebruik van de JOIN syntax overbodig.
dankje :) Perfect nu!

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
SELECT 
                        p.productName,
                        p.productDescription,
                        p.productPhoto,
                        p.productCategoryID,
                        p.productColorID,
                        p.productSizeID,
                        p.productPrice,
                        p.productStock,
                        p.productAdded,
                        c.categoryName,
                        co.colorName,
                        s.sizeName
                        
                    FROM 
                        products AS p
                        
                        LEFT OUTER JOIN categories AS c
                        ON p.productCategoryID = c.categoryID
                        
                        LEFT OUTER JOIN colors AS co
                        ON p.productColorID = co.colorID
                        
                        LEFT OUTER JOIN sizes AS s
                        ON p.productSizeID = s.sizeID
                        
                    AND
                        p.productID = ".mysql_real_escape_string($productID).";
Pagina: 1