Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 11:41 AM
Augustus
Guest
 
Posts: n/a
Default [SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...

Hiya!

I have a question:

I have 2 tables... "Inventory" and "Price"

These tables are used by 2 restaurants... the inventory has what they sell,
and prices has the corresponding prices to those items for each location.

What I want to do:
I want to get a list of all items in the "Inventory" table and the
corresponding price from the "Price" table for a location, unless the column
"SHOW" (in the "Price" table) is set to "N"

It might sound like "Just use a LEFT JOIN"... but if there is no entry for
the item in the "Price" table, I still want a result

I tried using:
===============
select i.name, p.price1
from inventory i
LEFT JOIN prices p
ON p.itemID=i.itemID
where (
(i.itemType=7)
AND (i.canusa='usa')
AND (p.storeID=99)
AND (p.show='y')
)

===============

But that only returns the items that have prices set up in the prices
table...

So I tried adding
===============
OR (NOT EXISTS (SELECT * FROM prices where (p.itemID=i.itemID) AND
(i.itemType=7))
)
===============

to the end of the WHERE conditions, but this returns everything in the
inventory table

I then tried changing the whole statement to:

===============
select i.name, p.price1
from inventory i
LEFT JOIN prices p
ON p.itemID=i.itemID
where (
(i.itemType=7)
AND (i.canusa='usa')
AND (p.storeID=99)
AND (p.show='y')
)
OR (i.itemType=7)
AND (i.canusa='usa')
AND (NOT EXISTS (SELECT * FROM prices where (p.itemID=i.itemID) AND
(i.itemType=7)))
===============

Now that does work... BUT, I am repeating some of the conditions of the
WHERE statement: (i.itemType=7) AND (i.canusa='usa')
And I am wondering if this is the most efficient way of doing it, or if its
just some bandaid approach that works and the true logic has escaped me?
(and is the "not exists" condition going to give some extra load the server
or not)

Any help is appreciated

Thanks,

Clint






  #2  
Old July 19th, 2005, 11:41 AM
Chris Hohmann
Guest
 
Posts: n/a
Default Re: [SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...

"Augustus" <Imperial.Palace@Rome.com> wrote in message
news:bvrtci$10f5pa$1@ID-97594.news.uni-berlin.de...[color=blue]
> Hiya!
>
> I have a question:
>
> I have 2 tables... "Inventory" and "Price"
>
> These tables are used by 2 restaurants... the inventory has what they[/color]
sell,[color=blue]
> and prices has the corresponding prices to those items for each[/color]
location.[color=blue]
>
> What I want to do:
> I want to get a list of all items in the "Inventory" table and the
> corresponding price from the "Price" table for a location, unless the[/color]
column[color=blue]
> "SHOW" (in the "Price" table) is set to "N"
>
> It might sound like "Just use a LEFT JOIN"... but if there is no entry[/color]
for[color=blue]
> the item in the "Price" table, I still want a result
>
> I tried using:
> ===============
> select i.name, p.price1
> from inventory i
> LEFT JOIN prices p
> ON p.itemID=i.itemID
> where (
> (i.itemType=7)
> AND (i.canusa='usa')
> AND (p.storeID=99)
> AND (p.show='y')
> )
>
> ===============
>
> But that only returns the items that have prices set up in the prices
> table...
>
> So I tried adding
> ===============
> OR (NOT EXISTS (SELECT * FROM prices where (p.itemID=i.itemID) AND
> (i.itemType=7))
> )
> ===============
>
> to the end of the WHERE conditions, but this returns everything in the
> inventory table
>
> I then tried changing the whole statement to:
>
> ===============
> select i.name, p.price1
> from inventory i
> LEFT JOIN prices p
> ON p.itemID=i.itemID
> where (
> (i.itemType=7)
> AND (i.canusa='usa')
> AND (p.storeID=99)
> AND (p.show='y')
> )
> OR (i.itemType=7)
> AND (i.canusa='usa')
> AND (NOT EXISTS (SELECT * FROM prices where (p.itemID=i.itemID) AND
> (i.itemType=7)))
> ===============
>
> Now that does work... BUT, I am repeating some of the conditions of[/color]
the[color=blue]
> WHERE statement: (i.itemType=7) AND (i.canusa='usa')
> And I am wondering if this is the most efficient way of doing it, or[/color]
if its[color=blue]
> just some bandaid approach that works and the true logic has escaped[/color]
me?[color=blue]
> (and is the "not exists" condition going to give some extra load the[/color]
server[color=blue]
> or not)
>
> Any help is appreciated
>
> Thanks,
>
> Clint[/color]

Answered in m.p.i.asp.db. Please don't multi-post:
http://aspfaq.com/5003


  #3  
Old July 19th, 2005, 11:41 AM
Augustus
Guest
 
Posts: n/a
Default Re: [SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...


"Chris Hohmann" <nospam@thankyou.com> wrote in message
news:%23Aupdy36DHA.1636@TK2MSFTNGP12.phx.gbl...[color=blue]
> "Augustus" <Imperial.Palace@Rome.com> wrote in message
> news:bvrtci$10f5pa$1@ID-97594.news.uni-berlin.de...
>
> Answered in m.p.i.asp.db. Please don't multi-post:
> http://aspfaq.com/5003[/color]

Thanks Chris

I originally meant to crosspost to the 2 groups... it fit in the other one
better than this one, but this one sees alot more activity and has answered
most of my SQL questions pretty quickly...

Clint


  #4  
Old July 19th, 2005, 11:41 AM
Ken Fine
Guest
 
Posts: n/a
Default Re: [SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...

Clint:

Be advised that people here hate it when you crosspost, even in
meaningful/relevant groups. It burns folks up to spend a lot of time
answering a question that's already been answered elsewhere.

We've all done it, so it isn't a capital crime, but it's probably best to
observe people's sensitivities in this matter.

-KF

"Augustus" <Imperial.Palace@Rome.com> wrote in message
news:bvs589$u3ei2$1@ID-97594.news.uni-berlin.de...[color=blue]
>
> "Chris Hohmann" <nospam@thankyou.com> wrote in message
> news:%23Aupdy36DHA.1636@TK2MSFTNGP12.phx.gbl...[color=green]
> > "Augustus" <Imperial.Palace@Rome.com> wrote in message
> > news:bvrtci$10f5pa$1@ID-97594.news.uni-berlin.de...
> >
> > Answered in m.p.i.asp.db. Please don't multi-post:
> > http://aspfaq.com/5003[/color]
>
> Thanks Chris
>
> I originally meant to crosspost to the 2 groups... it fit in the other one
> better than this one, but this one sees alot more activity and has[/color]
answered[color=blue]
> most of my SQL questions pretty quickly...
>
> Clint
>
>[/color]


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles