473,378 Members | 1,447 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,378 software developers and data experts.

Querying a join table for multiple parts

Seth Schrock
2,965 Expert 2GB
I have a situation where I have three tables: tblContacts and tblCategories in a many-to-many relationship joined through tblContactCategories (my join table). Lets say that I want to search my database for all contacts that have the categories "Student" and "Father". What is the best way to do this? Also, I don't want limited to just two categories. I had thought of using a subquery, but having more than two categories makes this tricky to be able to figure out. I have created some vba functions to which I pass the current contactID and the category that I want to check if they have that category and then I just put two of those in my WHERE clause, but I'm not sure that I like that option as I will be running this against 20,000+ records with each record having potentially four function calls. I'm just looking for the best method. The more ideas the better.
Oct 9 '15 #1

✓ answered by Rabbit

The way I usually handle something like this is
Expand|Select|Wrap|Line Numbers
  1. SELECT IDField
  2. FROM tableName
  3. WHERE Category IN ('a', 'b')
  4. GROUP BY IDField
  5. HAVING COUNT(*) >= 2
You can then join this to another table to pull the rest of the information you need.

6 852
Rabbit
12,516 Expert Mod 8TB
The way I usually handle something like this is
Expand|Select|Wrap|Line Numbers
  1. SELECT IDField
  2. FROM tableName
  3. WHERE Category IN ('a', 'b')
  4. GROUP BY IDField
  5. HAVING COUNT(*) >= 2
You can then join this to another table to pull the rest of the information you need.
Oct 9 '15 #2
Seth Schrock
2,965 Expert 2GB
That is exactly what I was looking for. Thank you so much Rabbit.
Oct 9 '15 #3
Rabbit
12,516 Expert Mod 8TB
You're quite welcome. If you want to make it dynamic, you might have to use some VBA. Off the top of my head, I'm thinking a LIKE comparison against a delimited string with all the options you're looking for and a call to VBA to return the number of options in the string for the COUNT(*) filter.
Oct 9 '15 #4
Rabbit
12,516 Expert Mod 8TB
Found a way to do it dynamically without VBA. Assuming you have a comma delimited string of the values you're querying for, you could use this:
Expand|Select|Wrap|Line Numbers
  1. SELECT IDField 
  2. FROM TableName
  3. WHERE (',' & Forms!Form3!Text2 & ',') Like ('*,' & Category & ',*')
  4. GROUP BY IDField
  5. HAVING COUNT(*)=(Len(Forms!Form3!Text2) - Len(Replace(Forms!Form3!Text2, ',', '')) + 1);
Oct 9 '15 #5
Seth Schrock
2,965 Expert 2GB
Thats even better. You are a genious! Thanks again rabbit.
Oct 9 '15 #6
NeoPa
32,556 Expert Mod 16PB
He's such a smart-arse really. Isn't he?

Lucky we have him here with us and not going to waste.

Nice one again Rabbit :-)
Oct 9 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Keith Simeon | last post by:
Hello all, I've recently started playing with Hibernate, and have a question regarding many-to-many relationships using a join or cross table (I've also heard these referred to as a transition...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
3
by: CanFlightSim | last post by:
I am not very strong in databases, so you must forgive any obvious stupidity. After having a good look at our Sales Managemend database, we decided there is something basially wrong. Our Agents...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
by: waNnY | last post by:
Hi all..i'm urgently need any attention here..pliz help to solve this problem.. The description of my problem is like dis >> let say now i have 100 length of a text. Now , i need to break it into...
1
by: salamy | last post by:
i want to querying a table using dropdown(Level)e.g 100l,200L
6
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
0
by: Todd Nichols | last post by:
I have the main form for the "one" side of the relationship and a subform for the "many" side, linked by the primary key of the main table to the foreign key of the join table (the many-side table). ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.