473,466 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Selecting several types

Hi all,

So I have these tables of items and item-types, and the items can be
of several types:

Items:
ID ; Name
1 ; Item1
2 ; Item2

ItemType:
ID ; Type
100 ; Type1
200 ; Type2
300 ; Type3

Now, to link these 2 I have a "linking" table:

LinkingTable:
ItemID ; ItemTypeID
1 ; 100
1 ; 200
2 ; 100
2 ; 300

Now I want to select all the items of type 100 or 200...

How can I do that without getting a duplicate row of item 1??

Thanks in advance,
Danny
Jun 27 '08 #1
4 1354
You can use DISTINCT:

SELECT DISTINCT I.item_id, I.item_name
FROM Items AS I
JOIN ItemTypes AS T
ON I.item_id = T.item_id
WHERE T.item_type_id IN (100, 200);

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Jun 27 '08 #2
>So I have these tables of items and item-types, and the items can be of several types: <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Sample data is also a good idea,
along with clear specifications. It is very hard to debug code when
you do not let us see it. If you want to learn how to ask a question
on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html

Here is a guess at what you meant to post:

CREATE TABLE Inventory -- collective noun for a set name
(item_id CHAR(15) NOT NULL PRIMARY KEY, --let's use GTIN
item_name VARCHAR(20) NOT NULL);

CREATE TABLE ItemTypes -- needs a better name; blood type? tarriff
type?
(item_type INTEGER NOT NULL PRIMARY KEY,
item_type_description VARCHAR(50) NOT NULL);

You do know that there is no such thing as a "type_id" -- an attribute
can be one or the other but not both.
>Now, to link these two tables I have a "linking" table: <<
That is a term from CODASYL databases; we have relations in SQL. They
usually have a proper name, like "Marriages" or "JobAssignments",
etc. I will make a guess at what you meant and add minimal DRI.

CREATE TABLE ItemClassifications
(item_id CHAR(15) NOT NULL
REFERENCES Inventory (item_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
item_type INTEGER NOT NULL
REFERENCES ItemTypes(item_type)
ON UPDATE CASCADE,
PRIMARY KEY (item_id, item_type));
>Now I want to select all the items of type 100 or 200...<<
SELECT DISTINCT item_id
FROM Inventory
WHERE item_type IN (100, 200);

You need to Google "Relational Division" for more genral solutions to
this kind of query.
Jun 27 '08 #3
Danny (ad*********@gmail.com) writes:
So I have these tables of items and item-types, and the items can be
of several types:

Items:
ID ; Name
1 ; Item1
2 ; Item2

ItemType:
ID ; Type
100 ; Type1
200 ; Type2
300 ; Type3

Now, to link these 2 I have a "linking" table:

LinkingTable:
ItemID ; ItemTypeID
1 ; 100
1 ; 200
2 ; 100
2 ; 300

Now I want to select all the items of type 100 or 200...

How can I do that without getting a duplicate row of item 1??
Another solution is to use EXISTS:

SELECT i.item_id, i.item_name
FROM items i
WHERE EXISTS (SELECT *
FROM linkingtable l
WHERE i.item_id = l.item_id
AND l.item_type_id IN (100, 200))

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #4
--CELKO-- wrote:
CREATE TABLE ItemClassifications
(item_id CHAR(15) NOT NULL
REFERENCES Inventory (item_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
item_type INTEGER NOT NULL
REFERENCES ItemTypes(item_type)
ON UPDATE CASCADE,
PRIMARY KEY (item_id, item_type));
>>Now I want to select all the items of type 100 or 200...<<

SELECT DISTINCT item_id
FROM Inventory
WHERE item_type IN (100, 200);
s/Inventory/ItemClassifications/

This will work if all you need is item_id. If you need some data from
the Inventory table as well, then use Plamen's or Erland's approach.
Jun 27 '08 #5

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

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
2
by: John | last post by:
Hello, I have a table called BUILDREQUESTS which I want to select from, depending on the project ID of each record. The Project ID field is indexed. (A) This query runs almost instantly: ...
0
by: brent | last post by:
Hi there... I have a stateless Session Bean issuing a select for a BLOB column. This bean is using a CachedRowSet. It seems to work for all other columns types other than a BLOB. Selecting a...
4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
3
by: sam.lumbroso | last post by:
I have the following object type created in Oracle (OCI8): create or replace type address as object ( address VARCHAR2(80), city VARCHAR2(80), state VARCHAR2(20), zip VARCHAR2(10) )
2
by: Phrogz | last post by:
Years ago I wrote a library to turn normal <selectsinto text inputs that the user can type in, with various features. In those days, I was working for an IE-centric company, so I didn't spend the...
1
by: kristof.bonnast | last post by:
Hi all, I'm have a console app which is called by a thirth party system over 20000 times a day. This console app links to a webservice on another server to capture information. This works fine...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I have to load 30,000 unique names into a combox. Filling a dataTable takes only a few milliseconds. But populating the combobox and displaying the list takes several seconds - way...
4
by: daanoz | last post by:
hey I'm trying to make a query which selects all rows that are not linked to the a certain number, i already managed to do this with a subquery, but this slows down the application, mainly caused...
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
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.