Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query syntax

Posted on 2009-07-12
7
Medium Priority
?
280 Views
Last Modified: 2012-05-07
I need to set up a query and I'm not sure of the syntax. Here is the schema scenario:
A 'Company' table with companyID and companyName.
A 'Delivery Areas' table with areaID and areaDescription.
A CompanyDeliveryCrossReference table with a refID (just autonumber), companyID, and areaID

The cross reference table will have an entry for each area that the company delivers within. What would a query look like if it were looking for companys that delivered in all:Area1, Area2, Area3?
Thanks!
Amy
0
Comment
Question by:AmyL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24836315
Like this:

SELECT C.*
FROM Company C, CompanyDeliveryCrossReference R
WHERE R.areaID IN (1,2,3) AND R.companyID=C.companyID

Open in new window

0
 
LVL 8

Accepted Solution

by:
Bobaran98 earned 1000 total points
ID: 24836321
Actually, sorry, you'd want to use the following query to avoid duplicates (because with the previous query, if a company served both area 1 and area 2, it would show up twice in the list).

By the way, the autonumber refID isn't necessary for your CompanyDeliveryCrossReference table, since companyID and areaID can serve as a dual key (it's a many-to-many relationship).  However, if you choose to use an autonumber key field, it won't hurt anything.

SELECT DISTINCT C.*
FROM Company C, CompanyDeliveryCrossReference R
WHERE R.areaID IN (1,2,3) AND R.companyID=C.companyID

Open in new window

0
 

Author Closing Comment

by:AmyL
ID: 31602652
Thanks!!
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:AmyL
ID: 24836528
Hi!
I just noticed that the IN (1,2,3) is like '1 OR 2 OR 3'. Actually what I need is '1 AND 2 AND 3'. Is there a slight modification that I can make to accomplish this?

Thanks!
Amy
0
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24836756
Ah, I see.  Well, that's a very different matter.  There are a few different ways to approach the problem, and probably none of them will look very pretty. :-)  What makes this ugly to code is the fact that you're trying to match against three separate records, which means you have to find someway of accessing the cross reference table three times simultaneously.

First off, about what I said earlier regarding CompanyDeliveryCrossReference and the autonumber key-- There actually is a very good reason why you want to use a dual key (companyID and areaID) rather than an arbitrary autonumber key, and that's the fact that you should never have more than one row with those two values-- it would be an unnecessary duplicate.  By making those two fields a dual key, you're telling your database to prevent the addition of duplicate rows (which, of course, you should be careful in your code to avoid anyway).

I say all that now because, by preventing duplicates, you can also do something like this to solve your problem:

SELECT C.*
 
FROM Company C
 
WHERE
  (
    (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=1 AND companyID=C.companyID)
    + (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=2 AND companyID=C.companyID)
    + (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=3 AND companyID=C.companyID)
    = 3
  )

Open in new window

0
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24836759
Sorry, I let my parentheses get out of control.  Probably wouldn't make any difference, except for readability, but below is what I meant to type! :-)

Hope this works for you!

SELECT C.*
 
FROM Company C
 
WHERE
  (
    (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=1 AND companyID=C.companyID)
    + (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=2 AND companyID=C.companyID)
    + (SELECT COUNT(*) FROM CompanyDeliveryCrossReference WHERE areaID=3 AND companyID=C.companyID)
  ) = 3

Open in new window

0
 

Author Comment

by:AmyL
ID: 24836955
Wow, clever!! Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question