Welcome to Dream.In.Code
Become an Expert!

Join 149,539 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,524 people online right now. Registration is fast and FREE... Join Now!




Dumb SQL

 
Reply to this topicStart new topic

Dumb SQL, (and dumb ASP)

klewlis
12 Jun, 2002 - 08:55 AM
Post #1

cur tu me vexas?
*****

Joined: 9 Nov, 2001
Posts: 1,723

Ok, normally I adore SQL. But this time I have to do something that it doesn't like. So I'm wondering if anyone has a um... *cleaner* way to do what I'm trying to do. First, an explanation. It's for a small (ie: low traffic) employment site; employers can post job listings with up to 3 categories, and users can post resumes with up to three categories. The problem comes when I try to *match* them because SQL won't let me say "if any of the resume categories match any of the listing categories" without long, drawn out OR statements. So here's what I have (ugh):

CODE

strSQL = "SELECT listing_id, position_name, post_date FROM table WHERE "

If category1 <> "0" Then
    strSQL = strSQL & "category1 = '" & category1 & "' OR category2 = '" & category1 & "' OR category3 = '" & category1 & "'"
End If

If category2 <> "0" Then
    If category1 <> "0" Then
 strSQL = strSQL & " OR "
    End If
    strSQL = strSQL & "category1 = '" & category2 & "' OR category2 = '" & category2 & "' OR category3 = '" & category2 & "'"
End If

If category3 <> "0" Then
    If category1 <> "0" OR category2 <> "0" Then
 strSQL = strSQL & " OR "
    End If
    strSQL = strSQL & "category1 = '" & category3 & "' OR category2 = '" & category3 & "' OR category3 = '" & category3 & "'"
End If

strSQL = strSQL & " ORDER BY post_date DESC"


I could combine the three If's into a loop, but then I'll have to add a junk criteria to make the OR syntax work... ugh....

Anyone have a better idea? it's soooo ugly right now! And I have about 5 different versions of the same ugliness for different purposes (ie: notifying employers vs notifying seekers, and all their variations!).
User is offlineProfile CardPM
+Quote Post

malkiri
RE: Dumb SQL
12 Jun, 2002 - 10:11 AM
Post #2

D.I.C Regular
Group Icon

Joined: 29 Mar, 2001
Posts: 364



Thanked: 1 times
Dream Kudos: 4
My Contributions
I think I know how you could make this prettier. Try using the IN keyword. As in:

CODE

...
WHERE category1 IN ('hi', 'yo', 'wassup')


Applied to your problem:

CODE

strSQL = "SELECT listing_id, position_name, post_date FROM table WHERE "

strSQL = strSQL & "category1 IN ('" & category1 & "','" & category2 & "','" & category3 "')"


You'll probably want some variation on this. Lemme know if that works.
User is offlineProfile CardPM
+Quote Post

RE: Dumb SQL
12 Jun, 2002 - 10:13 AM
Post #3




Joined: --
Posts: 0

K,

Even though you were dissing me yesterday I'll still help you... wink2.gif

What you want to do is use a nested SELECT (unless you are in MySQL, which would force you to use a second query and concatenate the categories together)

SELECT id, category, name FROM resume WHERE resume.category IN (SELECT category1, category2, category3 FROM job WHERE id = x)

this would allow you to specify which job req and search for any resumes that match the category. You could reverse the tables and allow the resume submitter to search for jobs that match his categories, but always build tools from the perspective of who is paying (which in most cases like this would be the prospective employer).

On further review this won't do exactly what you asked.

I think you need to either do some sort of complex matching code (there may be a function but I can't think of it imediately) or do two queries:

SELECT cat1, cat2, cat3 FROM job WHERE id = x
WHILE ...
str = cat1 + ', " + cat2 + ', " + cat3;

SELECT id, name FROM resume WHERE cat1 IN(str) OR cat2 IN(str) OR cat3 IN(str)

Looks like malkiri was writing at the same time as me... pretty much the same answer

Hoopster
User is offlinePM
+Quote Post

malkiri
RE: Dumb SQL
12 Jun, 2002 - 10:14 AM
Post #4

D.I.C Regular
Group Icon

Joined: 29 Mar, 2001
Posts: 364



Thanked: 1 times
Dream Kudos: 4
My Contributions
Hehe...I win! cool.gif
User is offlineProfile CardPM
+Quote Post

klewlis
RE: Dumb SQL
12 Jun, 2002 - 11:43 AM
Post #5

cur tu me vexas?
*****

Joined: 9 Nov, 2001
Posts: 1,723

Well, that helped a lot. I still had to mess it up a bit to get rid of the 0's, but here's how it looks now:

CODE

strSQL = "SELECT listing_id, position_name, post_date FROM table WHERE "

strSQL = strSQL & "(category1 <> '0' AND category1 IN ('" & category1 & "', '" & category2 & "', '" & category3 & "')) "

strSQL = strSQL & "OR (category2 <> '0' AND category2 IN ('" & category1 & "', '" & category2 & "', '" & category3 & "')) "

strSQL = strSQL & "OR (category3 <> '0' AND category3 IN ('" & category1 & "', '" & category2 & "', '" & category3 & "')) "

strSQL = strSQL & " ORDER BY post_date DESC"


Thanks for both of your help! Now the arduous task of converting all the variations... :P
User is offlineProfile CardPM
+Quote Post

RE: Dumb SQL
12 Jun, 2002 - 12:02 PM
Post #6




Joined: --
Posts: 0

put it in a function and you only have to edit it once...

Of course you'll have to go change all the places you call it one time but after that if you make changes it would be easy. You can even pass the table name to the function and that way you could possibly use the same function for searches by individuals and companies.
User is offlinePM
+Quote Post

klewlis
RE: Dumb SQL
12 Jun, 2002 - 12:30 PM
Post #7

cur tu me vexas?
*****

Joined: 9 Nov, 2001
Posts: 1,723

no, it actually has to be different each time...

for example, in one place I'm displaying a list of matches, in another I'm emailing to all the matches, etc. On one, I have more than three categories because I take all the matching categories for all the positions for each employer, so there could be any number of them. Different tables, different columns, etc. None of them match exactly, so it's not worth making a function or subroutine (If I did, I'd end up with half a million parameters... :)

Anyway, I have them all fixed up now and am pleased!
User is offlineProfile CardPM
+Quote Post

RE: Dumb SQL
12 Jun, 2002 - 12:52 PM
Post #8




Joined: --
Posts: 0

Glad to hear it!

You still could use a function to retrieve the data and use it one way (mail) in one place and another way (display) in another. Since you have 3 cat's in each all you would have needed to do is name the cat's the same thing and you only need to pass a couple variables.

Probably not worth the effort now but possibly something to consider in future table design.

In my head I see passing an id, a table name and a parameter or two to tell it what to return... i don't know your design though and in my head is a pretty scary place...

Hoopster
User is offlinePM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month