473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

[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


Jul 19 '05 #1
3 1415
"Augustus" <Im*************@Rome.com> wrote in message
news:bv*************@ID-97594.news.uni-berlin.de...
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
Jul 19 '05 #2

"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Augustus" <Im*************@Rome.com> wrote in message
news:bv*************@ID-97594.news.uni-berlin.de...

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
Jul 19 '05 #3
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" <Im*************@Rome.com> wrote in message
news:bv************@ID-97594.news.uni-berlin.de...

"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Augustus" <Im*************@Rome.com> wrote in message
news:bv*************@ID-97594.news.uni-berlin.de...

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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nils Magnus Englund | last post by:
Hi! I have a large table in SQL Server 2000 with a datetime-column 'dt'. I want to select all rows from that table, excluding days which fall on holidays or weekends. What is the best way to...
6
by: Brad | last post by:
I have an sql query that has specific criteria (like state='PA' or state = 'NJ'...) and would like to be able to have the user specify the criteria dynamically either through the web or from...
14
by: diskoduro | last post by:
Hi!! Years ago I built a database to control the production of a little factory. The users wanted to work in a Windows Net workgroup so I created an mdb with all the tables and data an after...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
2
by: Ivor Somerset | last post by:
Hi, I've an Access DB table ("Groups") where data are as follow: Id Group Rank Item 1 1 1 7364 2 1 2 283 3 1 3 34888 4 2 1 277 5 2 2 8233
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
2
by: Steve | last post by:
I have zero experience with ODBC. If I have an Access frontend connected to a SQL Database using ODBC, are the tables connected like a frontend/backend Access database where the the tables you see...
2
by: timor.super | last post by:
Hi group, In my database, I have a table with fields like this : id | title | xml ------------------------------------ 1 | title1 | <datas><data><item...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.