[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

MySQL Query WHERE (boolean eqn) question

Query1 yields no matches:

SELECT PR.*
   FROM tblProducts AS PR, tblStylesXProducts AS SXP
    WHERE (
              PR.PrimaryStyleID=5  
           OR
              (SXP.StyleID= 5 AND SXP.ProductID=PR.ID)
                   )

Query 2 yields the proper matches
SELECT PR.*
   FROM tblProducts AS PR
    WHERE (
              PR.PrimaryStyleID=5
                   )

Table  tblStylesXProducts  (a cross-products table to allow for "secondary" styles for each product)   has no records in it presently.
How am i misunderstanding the specification of WHERE to achieve the   OR   effect.
My understanding is that if the table is empty, then it would have no effect on allowing the first part of the WHERE to contribute as desired.

An aside:  this was designed this way to simplify the most common case where there are no secondary styles, and thus admin entry is simple in the tblProducts.    An alternative would be to remove the  PrimaryStyleID field and make that a flag/boolean within  tblStylesXProducts to indicate that this record's combination is the Primary Style.  (but then each product would have to generate a separate record in the tblStylesXProducts table)

tblStyles.gif
tblProducts.gif
0
willsherwood
Asked:
willsherwood
2 Solutions
 
RurneCommented:
Without seeing the actual data definition language used to build these tables, I can only assume what's happening is your implicit cross-join is matching tblProducts.ID=tblStylesXProducts.ID.  Not good.

What are the results of using explicit join notation?  Try the following:


SELECT PR.*
   FROM tblProducts AS PR INNER JOIN tblStylesXProducts AS SXP
   ON PR.ID=SXP.ProductID
    WHERE (
              PR.PrimaryStyleID=5  
           OR
              SXP.StyleID=5
                   )
0
 
awking00Commented:
Try the attached query.
where-exists.txt
0
 
willsherwoodAuthor Commented:
thanks for your help!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now