Skip to main content
Inspiring
October 3, 2012
Question

left join help or alternatives

  • October 3, 2012
  • 2 replies
  • 2813 views

let me explain what i have set up

i have a product detail page that shows the product details and the sizes, the sizes a pulled in from a table

i have the following table joined

prod table

ID (prod)

name

CatID

Size Table

SizeID

Size

Stock Table

StockID

ID (prod)

SizeID

Stock

i have a select list that gets the sizes for each product

      <option value="Select Size">Select Size</option>

      <?php

                $query2 = sprintf("

                                        SELECT DISTINCT

                                                  stock.StockID, size.Size

                                        FROM

                                                  beauProd AS prod

                                                  LEFT JOIN beauStock AS stock ON prod.ID = stock.ID

                                                  LEFT JOIN beauSizeList AS size ON stock.SizeID = size.SizeID

                                        WHERE

                                                  prod.ID = '%s' AND stock.Stock > 0

                                        ORDER BY

                                                  size.SizeID ASC", GetSQLValueString($var1_Recordset1, "int"));

                                        $results2 = mysql_query($query2);

                                        while($row2 = mysql_fetch_array($results2)){

                                                  ?>

      <option value="<?php echo $row2['Size']; ?>"><?php echo $row2['Size']; ?></option>

      <?php

                                        }

 

                ?>

    </select>

i want to know if there is another way of getting this acheived. The reason i ask is because someone else done this code and need to understand it or recreate it using this or another method.

thanks in advance

This topic has been closed for replies.

2 replies

Participating Frequently
October 3, 2012

Do you need an outer join at all? Does your LEFT table contain unmatched rows in the RIGHT table? That does not sound likely.

Inspiring
October 3, 2012

i have been doing debuggin since my post and am getting the following errors

these are the tables in the DB

poochieCat

CatID


poochieProd

ProdID

CatID


poochieProd

ProdID

CatID


poochieSizes

SizeID

Size


poochieStock

stockID

ProdID

sizeID

stock

Below is the select list contining the query

<select name="os0" class="text" id="selectSize">

      <option value="Select Size">Select Size</option>

      <?php

                $query2 = sprintf("

                                        SELECT DISTINCT

                                                  stock.stockID, size.Size

                                        FROM

                                                  poochieProd AS prod

                                                  LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID

                                                  LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID

                                        WHERE

                                                  poochieProd.ProdID = '%s' AND stock.stock > 0

                                        ORDER BY

                                                  size.SizeID ASC", GetSQLValueString($var3_Recordset1, "int"));

                                        $results2 = mysql_query($query2);

                                        while($row2 = mysql_fetch_array($results2)){

                                                  ?>

      <option value="<?php echo $row2['Size']; ?>"><?php echo $row2['Size']; ?></option>

      <?php

                                        }

 

                ?>

    </select>

and this is the error that is echoing

Error in query: SELECT DISTINCT stock.stockID, size.Size FROM poochieProd AS prod LEFT JOIN poochieStock AS stock ON Prod.ID = stock.ID LEFT JOIN poochieSizes AS size ON stock.SizeID = size.SizeID WHERE poochieProd.ProdID = '-1' AND stock.stock > 0 ORDER BY size.SizeID ASC. Unknown table 'poochieProd' in where clause

Participating Frequently
October 3, 2012

>Unknown table 'poochieProd' in where clause

It's unknown because you created an alias for it in the FROM clause. You still have not answered as to why you are using LEFT joins.

Inspiring
October 3, 2012

i have run the script and i am getting the following error in the output

<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/users1/jonfort/public_html/designfreak/pooch/product-detail.php on line 132

Participating Frequently
October 3, 2012

>have run the script and i am getting the following error in the output

>

><b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/users1/jonfort>/public_html/designfreak/pooch/product-detail.php on line 132

Not being a PHP expert, my guess is that the query is empty. What is the database datatype of prod.ID ?

Inspiring
October 3, 2012

Not being a PHP expert, my guess is that the query is empty. What is the database datatype of prod.ID ?


int(11)

No
auto_increment

in the poochieProd table

and

varchar(80)latin1_swedish_ci
No

in the poochieStock table