473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[SQL] Matching One to many relationship, difficult.

Hi,

I shall try to be short and to the point, this is a problem that no
one IRL has yet been able to help me with thus far. Yet it feels as
thought this is something that many people must tackle often.

There are three tables with this simple structure:
_______________ _________
# items
item_id | name

# labels
label_id | name

# labels_relation ship
relationship_id | label_id | item_id
_______________ _________

Items contains rows of unique 'items'. And each item can have X number
of labels related to them; by using the relations table
"labels_relatio nship" which simply lists all the relationships between
a label and item with no limitations as to how many labels can be
assigned to an item.

What I need to fetch from the DB, is all items, that has a
relationship with a predefined set of "labels" saved in the
labels_relation ship-table.

I must be able to fetch the items that has an entry in
labels_relation ship for i.e: label_id 1 AND label_id 2 AND label_id 3.
If the item does not have an entry for ALL of these labels in
labels_relation ship it should be filtered out (not be selected).

My problem arises due to the fact that I have to match against
multiple rows in the same table. Me and several others have
experimented with several different approaches, and the latest theory
was that; if one create virtual columns for each label, it would be a
simple task of constructing a WHERE label_id=1 AND label_id=2 etc. But
that experiment ended up in the wall as well. All our Inner join-tests
also failed miserably.

I and my project group of fellow students need help from someone more
experienced in these things.

All replies, constructive pointers and other advice will be deeply
appreciated!

Oct 10 '07 #1
4 2255
well, the following strikes me as too inflexible, since it is specific to a
group of exactly 3 labels - but maybe it will spark ideas that help you come
up with a better solution:

SELECT tblItems.item_i d, tblLabels_relat ionship.label_i d
FROM tblItems INNER JOIN tblLabels_relat ionship ON tblItems.item_i d =
tblLabels_relat ionship.item_id
WHERE (((tblItems.ite m_id) In (SELECT tblItems.item_i d
FROM tblItems INNER JOIN tblLabels_relat ionship ON tblItems.item_i d =
tblLabels_relat ionship.item_id
WHERE (((tblItems.ite m_id) In (SELECT tblItems.item_i d
FROM tblItems INNER JOIN tblLabels_relat ionship ON tblItems.item_i d =
tblLabels_relat ionship.item_id
WHERE (((tblLabels_re lationship.labe l_id)=1));)) AND
((tblLabels_rel ationship.label _id)=2));
)) AND ((tblLabels_rel ationship.label _id)=3));

hth
"Varghjärta " <va********@gma il.comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
Hi,

I shall try to be short and to the point, this is a problem that no
one IRL has yet been able to help me with thus far. Yet it feels as
thought this is something that many people must tackle often.

There are three tables with this simple structure:
_______________ _________
# items
item_id | name

# labels
label_id | name

# labels_relation ship
relationship_id | label_id | item_id
_______________ _________

Items contains rows of unique 'items'. And each item can have X number
of labels related to them; by using the relations table
"labels_relatio nship" which simply lists all the relationships between
a label and item with no limitations as to how many labels can be
assigned to an item.

What I need to fetch from the DB, is all items, that has a
relationship with a predefined set of "labels" saved in the
labels_relation ship-table.

I must be able to fetch the items that has an entry in
labels_relation ship for i.e: label_id 1 AND label_id 2 AND label_id 3.
If the item does not have an entry for ALL of these labels in
labels_relation ship it should be filtered out (not be selected).

My problem arises due to the fact that I have to match against
multiple rows in the same table. Me and several others have
experimented with several different approaches, and the latest theory
was that; if one create virtual columns for each label, it would be a
simple task of constructing a WHERE label_id=1 AND label_id=2 etc. But
that experiment ended up in the wall as well. All our Inner join-tests
also failed miserably.

I and my project group of fellow students need help from someone more
experienced in these things.

All replies, constructive pointers and other advice will be deeply
appreciated!

Oct 10 '07 #2
On 10 okt, 03:27, Varghjärta <varghja...@gma il.comwrote:
Hi,

I shall try to be short and to the point, this is a problem that no
one IRL has yet been able to help me with thus far. Yet it feels as
thought this is something that many people must tackle often.

There are three tables with this simple structure:
_______________ _________
# items
item_id | name

# labels
label_id | name

# labels_relation ship
relationship_id | label_id | item_id
_______________ _________

Items contains rows of unique 'items'. And each item can have X number
of labels related to them; by using the relations table
"labels_relatio nship" which simply lists all the relationships between
a label and item with no limitations as to how many labels can be
assigned to an item.

What I need to fetch from the DB, is all items, that has a
relationship with a predefined set of "labels" saved in the
labels_relation ship-table.

I must be able to fetch the items that has an entry in
labels_relation ship for i.e: label_id 1 AND label_id 2 AND label_id 3.
If the item does not have an entry for ALL of these labels in
labels_relation ship it should be filtered out (not be selected).

My problem arises due to the fact that I have to match against
multiple rows in the same table. Me and several others have
experimented with several different approaches, and the latest theory
was that; if one create virtual columns for each label, it would be a
simple task of constructing a WHERE label_id=1 AND label_id=2 etc. But
that experiment ended up in the wall as well. All our Inner join-tests
also failed miserably.

I and my project group of fellow students need help from someone more
experienced in these things.

All replies, constructive pointers and other advice will be deeply
appreciated!
You want to select items, based upon the number of matches in
labels_relation ship.
First count those matches (I'm pretty sure you know the summary
function to use). Then apply a select (but it's called differently) to
the
summarised results.


Oct 10 '07 #3
On 10 Oct, 02:27, Varghjärta <varghja...@gma il.comwrote:
Hi,

I shall try to be short and to the point, this is a problem that no
one IRL has yet been able to help me with thus far. Yet it feels as
thought this is something that many people must tackle often.

There are three tables with this simple structure:
_______________ _________
# items
item_id | name

# labels
label_id | name

# labels_relation ship
relationship_id | label_id | item_id
_______________ _________

Items contains rows of unique 'items'. And each item can have X number
of labels related to them; by using the relations table
"labels_relatio nship" which simply lists all the relationships between
a label and item with no limitations as to how many labels can be
assigned to an item.

What I need to fetch from the DB, is all items, that has a
relationship with a predefined set of "labels" saved in the
labels_relation ship-table.

I must be able to fetch the items that has an entry in
labels_relation ship for i.e: label_id 1 AND label_id 2 AND label_id 3.
If the item does not have an entry for ALL of these labels in
labels_relation ship it should be filtered out (not be selected).

My problem arises due to the fact that I have to match against
multiple rows in the same table. Me and several others have
experimented with several different approaches, and the latest theory
was that; if one create virtual columns for each label, it would be a
simple task of constructing a WHERE label_id=1 AND label_id=2 etc. But
that experiment ended up in the wall as well. All our Inner join-tests
also failed miserably.

I and my project group of fellow students need help from someone more
experienced in these things.

All replies, constructive pointers and other advice will be deeply
appreciated!
What is the field relationship_id for?

Oct 10 '07 #4
On Oct 9, 6:27 pm, Varghjärta <varghja...@gma il.comwrote:
Hi,

I shall try to be short and to the point, this is a problem that no
one IRL has yet been able to help me with thus far. Yet it feels as
thought this is something that many people must tackle often.

There are three tables with this simple structure:
_______________ _________
# items
item_id | name

# labels
label_id | name

# labels_relation ship
relationship_id | label_id | item_id
_______________ _________

Items contains rows of unique 'items'. And each item can have X number
of labels related to them; by using the relations table
"labels_relatio nship" which simply lists all the relationships between
a label and item with no limitations as to how many labels can be
assigned to an item.

What I need to fetch from the DB, is all items, that has a
relationship with a predefined set of "labels" saved in the
labels_relation ship-table.

I must be able to fetch the items that has an entry in
labels_relation ship for i.e: label_id 1 AND label_id 2 AND label_id 3.
If the item does not have an entry for ALL of these labels in
labels_relation ship it should be filtered out (not be selected).

My problem arises due to the fact that I have to match against
multiple rows in the same table. Me and several others have
experimented with several different approaches, and the latest theory
was that; if one create virtual columns for each label, it would be a
simple task of constructing a WHERE label_id=1 AND label_id=2 etc. But
that experiment ended up in the wall as well. All our Inner join-tests
also failed miserably.

I and my project group of fellow students need help from someone more
experienced in these things.

All replies, constructive pointers and other advice will be deeply
appreciated!
Try something like this:

SELECT i.item_id,i.nam e
FROM items i
WHERE EXISTS (SELECT lr1.item_id
FROM labels_relation ship lr1
WHERE i.item_id = lr1.item_id
AND
lr1.label_id IN (SELECT lr2.label_id
FROM labels_relation ship lr2
WHERE lr2.label_id IN (484,501,505))
GROUP BY lr1.item_id
HAVING COUNT(label_id) = 3)

It uses group by and counting the label_ids in the groups.

This problem is a "relational division" problem. Google for "sql
relational division" for more info on the topic. Relational division
problems are rare and can be quite difficult.

Oct 16 '07 #5

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

Similar topics

15
2141
by: Scott Auge | last post by:
I am looking for comments on something that lets me abstract database updates in an object. Lemme explain what I am thinking: Lets say I have an object Person with... SetFirstName() SetLastName()
9
3402
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in a triangle. Like this member A -> B->C A give his appartment to B. B gives his appartment to C and finally C gives his appartment to A Soo my query looks for matching parameters like rooms, location, size
3
1520
by: Metal Dave | last post by:
Hi all, Currently our product has a setup that stores information about transactions in a transaction table. Additionally, certain transactions pertain to specific people, and extra information is stored in another table. So for good or ill, things look like this right now: create table TransactionHistory ( TrnID int identity (1,1), TrnDT datetime,
2
3413
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I want them to be linked to the "main" table. Problem is, it won't let me run the SQL run as long as the one-many relationship is established in the relationship window. The SQL is:
2
3101
by: Terry | last post by:
Hello, I wonder if anyone can shed light on this problem for me. I have an Access 97 front end with an SQL 2000 database. There is a Business main form with an Owner subform and corresponding tables of the same names. A third table BusinessRel records the BusinessID (linked to Business table) and OwnerID (linked to Owner table). This is what happens when the Business main form is loaded. I enter the BusinessID and other stuff, then as...
59
7490
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's invalid operation or invalid parameter, I'm
3
6114
by: starman7 | last post by:
I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the designers in the designers table (which has fields like: id, fname, lname). How can my select return the possibly several designers for a product, with the rest of the row data, like price, name, etc.?
6
481
by: MVM | last post by:
Hi, I am attempting to run a query in MS SQL server between two tables that have a one to many relationship. The tables are linked on GID. What I want is one instance of every record from Table 1 even if there isn't a record in Table 2; and even if there are multiple records in Table 2. Table 1: GID Parcel
0
20327
by: Medhatithi | last post by:
Hi, I have been in several ways benefiited from this site. I would like to share some sql tuning techniques(simple, but effective) with you all. SQL Tuning Tips Oracle Tips Session #6 3/31/98 Kathy Gleeson ________________________________________
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.