Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > [SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...

Reply
Thread Tools

[SQL] Selecting item from table, whether or not it has corresponding entry in 2nd table...

 
 
Augustus
Guest
Posts: n/a
 
      02-04-2004
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






 
Reply With Quote
 
 
 
 
Chris Hohmann
Guest
Posts: n/a
 
      02-05-2004
"Augustus" <> wrote in message
news:bvrtci$10f5pa$...
> 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


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


 
Reply With Quote
 
 
 
 
Augustus
Guest
Posts: n/a
 
      02-05-2004

"Chris Hohmann" <> wrote in message
news:%...
> "Augustus" <> wrote in message
> news:bvrtci$10f5pa$...
>
> Answered in m.p.i.asp.db. Please don't multi-post:
> http://aspfaq.com/5003


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


 
Reply With Quote
 
Ken Fine
Guest
Posts: n/a
 
      02-05-2004
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" <> wrote in message
news:bvs589$u3ei2$...
>
> "Chris Hohmann" <> wrote in message
> news:%...
> > "Augustus" <> wrote in message
> > news:bvrtci$10f5pa$...
> >
> > Answered in m.p.i.asp.db. Please don't multi-post:
> > http://aspfaq.com/5003

>
> 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
>
>



 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Display pictures corresponding to database entry. Martin Jay HTML 6 05-15-2006 10:01 PM
not all values have corresponding parent values Jim in Arizona ASP .Net 4 08-23-2005 11:29 PM
Item cannot be found in the collection corresponding to the requested name or ordinal. Dthmtl ASP General 4 02-24-2005 04:40 PM
Storage size of files not corresponding with HDD properties =?Utf-8?B?U2hhd24=?= MCSE 2 11-16-2004 02:30 PM
Item cannot be found in the collection corresponding to the requested name or ordinal CJM ASP General 2 11-09-2004 04:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57