473,548 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ 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 1359
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_descr iption 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 ItemClassificat ions
(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*********@gm ail.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****@sommarsk og.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 ItemClassificat ions
(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/ItemClassificat ions/

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
2512
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 select a type on the basis of its traits. More specifically, I would like to select an unsigned integer type on the basis of the number of bits I need...
2
2058
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: SELECT BR.REQID FROM BUILDREQUESTS BR WHERE BR.PROJECTID IN...
0
2414
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 BLOB column, a SQLExcpetion when the CachedRowSet is populated. Anyone else run in to this situation? ENV: WSAD 5.0.1 running on WIN2K or RH9...
4
4708
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 athletes.  I have now learned how to join two different tables in a query so that I might generate a report. Specifically, student athletes at a...
3
3192
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
1620
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 effort to make it cross browser. (Bad me!) Now I need the functionality of that library, but I need it for FF or Safari. Specifically, I want the...
1
3024
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 for several weeks, but after some time I receive following error: System.InvalidOperationException: Unable to generate a temporary class...
4
5094
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 too long. A user selects a name from the combobox and runs a query. Originally, this combox was on a form in an MS Access ADP which was linked...
4
1237
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 because i have more then 100k rows. (simplefied) table ############################ # RowId # MessageId # Type # ############################...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.