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.
The way I usually handle something like this is - SELECT IDField
-
FROM tableName
-
WHERE Category IN ('a', 'b')
-
GROUP BY IDField
-
HAVING COUNT(*) >= 2
You can then join this to another table to pull the rest of the information you need.
6 855
The way I usually handle something like this is - SELECT IDField
-
FROM tableName
-
WHERE Category IN ('a', 'b')
-
GROUP BY IDField
-
HAVING COUNT(*) >= 2
You can then join this to another table to pull the rest of the information you need.
That is exactly what I was looking for. Thank you so much Rabbit.
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.
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: - SELECT IDField
-
FROM TableName
-
WHERE (',' & Forms!Form3!Text2 & ',') Like ('*,' & Category & ',*')
-
GROUP BY IDField
-
HAVING COUNT(*)=(Len(Forms!Form3!Text2) - Len(Replace(Forms!Form3!Text2, ',', '')) + 1);
Thats even better. You are a genious! Thanks again rabbit.
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 :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
by: salamy |
last post by:
i want to querying a table using dropdown(Level)e.g 100l,200L
|
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...
|
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...
|
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). ...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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...
|
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,...
| |