We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

MySQL Query  WHERE (boolean eqn)  question

willsherwood
willsherwood asked
on
Medium Priority
560 Views
Last Modified: 2012-05-06
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
Comment
Watch Question

Commented:
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
                   )

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
awking00Information Technology Specialist
CERTIFIED EXPERT
Commented:
Try the attached query.
where-exists.txt

Author

Commented:
thanks for your help!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.