Skip to main content
Inspiring
March 3, 2008
Question

Advanced Sarch

  • March 3, 2008
  • 20 replies
  • 2641 views
Hi,

I would appreciate some help with a query...
I have a table with 3 rows companyid, categoryid and categoryType
This is basically bunch of different category ids in one row and each company gets a set of categoryIDs
Looks something like this
companyID CatID CatType
5225 10 LOC
5225 12 LOC
5225 150 TYPE
5225 214 DUR
So i need to make a search with different options to find companies that matches these options
My dilemma is how can I search same row for all these options
If I do find where (( catID = 12 or catID = 10) and (catType = 'LOC')) then I can't match the other category types since LOC type has already narrow down the results?????
This topic has been closed for replies.

20 replies

TiGGiAuthor
Inspiring
March 30, 2008
Thanks Phil,

So do I need to loop through every user and check if they have something selected for each category, and then based on that I include the condition?

Participating Frequently
March 25, 2008
I suppose this equivalent would do the same thing.

SELECT DISTINCT e.employerID, u.UserID
FROM employer e, employer_CAT ec, User_CAT uc, employer_LOC el, User_LOC ul, employer_DUR ed, User_DUR ud, Users u
WHERE e.employerID = ec.employerID
AND ec.CAT = uc.CAT
AND e.employerID = el.employerID
AND el.LOC = ul.LOC
AND e.employerID = ed.employerID
AND ed.DUR = ud.DUR
AND uc.UserID = u.UserID
AND ul.UserID = u.UserID
AND ud.UserID = u.UserID

Phil
TiGGiAuthor
Inspiring
March 29, 2008
Hey Phil, thank you again for your help. Can I bother you once more?
I am gonna use the query you suggested last, I was wondering what happens if user hasn't selected one of the categories? Isn't this going to bring back incorrect result?


quote:

Originally posted by: paross1
I suppose this equivalent would do the same thing.

SELECT DISTINCT e.employerID, u.UserID
FROM employer e, employer_CAT ec, User_CAT uc, employer_LOC el, User_LOC ul, employer_DUR ed, User_DUR ud, Users u
WHERE e.employerID = ec.employerID
AND ec.CAT = uc.CAT
AND e.employerID = el.employerID
AND el.LOC = ul.LOC
AND e.employerID = ed.employerID
AND ed.DUR = ud.DUR
AND uc.UserID = u.UserID
AND ul.UserID = u.UserID
AND ud.UserID = u.UserID

Phil


Participating Frequently
March 30, 2008
All of the conditions in the query would have to be "true" to return a given row. If you restrict the values further in the where clause with specific values of DUR, CAT, LOC, or UserID, then you would get even fewer "matches". However, the maximum number of rows would be returned where all of the existing conditions in the where clause are met.

I don't know how else to answer your question at this point.

Phil
TiGGiAuthor
Inspiring
March 25, 2008
Hey paross, thanks for your help again...
in the SELECT DISTINCT e.employerID, u.UserID
FROM employer e
don't I need the USER table in FROM also?
Participating Frequently
March 25, 2008
It is... it is INNER JOINed ...

....
INNER JOIN Users u ON uc.UserID = u.UserID
AND ul.UserID = u.UserID
AND ud.UserID = u.UserID


Phil
TiGGiAuthor
Inspiring
March 25, 2008
The query that I posted is the one I used before the normalizing tables. Now that the tables are normalized I need help with the new query.

I don't know if there's an easy way to get tables structure but this is the basic layout:

[dbo].[Users]
UserID(int), UserMail,UserPassword....

[dbo].[User_LOC]
UserID(int), LOC(int)

[dbo].[User_CAT]
UserID(int), CAT(int)

[dbo].[User_DUR]
UserID(int), DUR(int)

Then I have the employer tables:

[dbo].[employer]
employerID(int), ....

[dbo].[employer_LOC]
employerID(int), LOC(int)

[dbo].[employer_CAT]
employerID(int), CAT(int)

[dbo].[employer_DUR]
employerID(int), DUR(int)


so for each user I need to take the values in User_LOC, User_CAT, User_DUR and find a match in the employer_LOC, employer_CAT, employer_DUR and return the employerID as result
Participating Frequently
March 25, 2008
Given your current structure, what does this query give you?

SELECT DISTINCT e.employerID, u.UserID
FROM employer e
INNER JOIN employer_CAT ec ON e.employerID = ec.employerID
INNER JOIN User_CAT uc ON ec.CAT = uc.CAT
INNER JOIN employer_LOC el ON e.employerID = el.employerID
INNER JOIN User_LOC ul ON el.LOC = ul.LOC
INNER JOIN employer_DUR ed ON e.employerID = ed.employerID
INNER JOIN User_DUR ud ON ed.DUR = ud.DUR
INNER JOIN Users u ON uc.UserID = u.UserID
AND ul.UserID = u.UserID
AND ud.UserID = u.UserID

NOTE: You would design your WHERE clause to specify the particular combinations of values for CAT, LOC, and DUR if necessary.

Phil
March 25, 2008
Yeah like Azadi says...only if you could just post your entire table structure...we could possibly understand what you have...so that we could help you out here.

sadb,
http://RapidshareFilms.com

Inspiring
March 25, 2008
from your query sample it still looks to me like you database is not
normalized... can you post your table structure for al tables involved
in your query?

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
TiGGiAuthor
Inspiring
March 25, 2008
What I am trying to acomplish is have a user search where user can pick different categories such as CAT, LOC, DUR.. and each of these categories have multiple selections. User save its search and his selections go into tables that have been normalized now. So I have a user table then I have a table for each category with userID and then the value of the each selection. On the other side I have the employer table which has related tables for it's set of categories.

What I need is to take a user saved selections and find employers that match those selections.

Right now what I have is pretty much what paross suggested
SELECT DISTINCT x.companyID
FROM your_table x
WHERE x.ACTIVE = 1
AND EXISTS (SELECT 1
FROM your_table y
WHERE y.CATID IN (1, 3, 17)
AND y.catTYPE = 'cat'
AND y.companyID = x.companyID)
AND EXISTS (SELECT 1
FROM your_table y
WHERE y.CATID = 1
AND y.catTYPE = 'DUR'
AND y.companyID = x.companyID)
AND EXISTS (SELECT 1
FROM your_table y
WHERE y.CATID IN (53, 54, 56, 59, 61, 1589, 1590, 1591)
AND y.catTYPE = 'LOC'
AND y.companyID = x.companyID)
Inspiring
March 25, 2008
can you please re-post you current query and explain again what output
you want? there are a lot of bits and pieces of code in this thread -
it's hard to put together what you might have now...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
TiGGiAuthor
Inspiring
March 24, 2008
paross1, took your advice and I normalized the dB, I think. I took all those different categories that had comma delimited lists in it and made it's own table which has CompanyID and then the id of category.
So now how do I optimize my query to work with this new setup?

Inspiring
March 5, 2008
TiGGi,
iirc, in at least 3 of your recent threads you have been advised by
numerous people, many of who happen to be experts in the field, to
normalize your database.

just that would make anyone else stop for a second and think carefully
about what they are doing. it's not just pushing air around - there is a
very good reason for this.

but you have been ignoring this advice, instead just repeating your
question about your "comma-delimited list of values in my field" and how
you are having trouble doing this and that with it.
stop. do not make your life any harder. take the time to
a) learn about data modelling and 3rd normal form
b) implement it in your database

there is a book called 'database design for mere mortals' or something
similar to that. get it. read it. keep it next to you at all times until
you know it by heart.

paross1 has given you good instructions on how to arrange your data so
that it conforms to the 3rd normal form. do it.

if you do not, you will find yourself wasting your time trying to do
something that would take just a minute to do in a normalized database.

normalize your db. it's not an option. it's a must.

hth

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/