If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > multiple queries on joined table - need a better way

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-06, 10:05
tim.pickup tim.pickup is offline
Registered User
 
Join Date: Jan 2006
Posts: 1
multiple queries on joined table - need a better way

Hi all,

I have a problem which I guess must be quite common, however I cant find any decent solution.

I have 3 tables..

contact (title,firstname,surname etc)

custom_field (field_name) - used to allow any number of custom fields to be added to a contact.

custom_field_value (contact_id,custom_field_id,value) - joining table which specifies an actual value for a custom field in relation to a specific contact.

The complication comes when I want to filter customers based on a number of custom_field_value.value's.

Currently I am selecting contacts and adding a where clause for each custom field that does an inner select and returns contact ids for all contacts with values in the custom_field_value table matching the required criteria.

each custom field im filtering on then adds a inner select and also a join.. this is running very slow !

Is there a better way to do this ?

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-30-06, 11:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by tim.pickup
Is there a better way to do this ?
yes

join the custom_field_value table to the contact table, use WHERE conditions to accept only those custom_field_value.value values that you're looking for, then do a GROUP BY on the contact columns, with a HAVING clause for COUNT(*) = N where N is the number of custom_field_value.value values searched for

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On