1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

Advanced AND and OR on simple structure

Discussion in 'SQL - Questions and Answers' started by buijze, Jun 25, 2009.

  1. buijze New Member

    Hi,

    For quite a simple table-structure, ie. Person, Criteria, and PersonCriteria (the combi-table), I have set up a query at the moment that selects alle persons that possess all selected criteria.

    #arguments.criteriaIDs# is a coldfusion list with one of more IDs

    The query itself looks like this at the moment:
    Select p.PersonID
    From Person p,
    ( Select Distinct PersonID, CriteriaID
    From PersonCriteria
    Where CriteriaID in (#arguments.criteriaIDs#)
    ) k
    Where p.PersonID= k.PersonID
    Group BY p.PersonID
    Having Count(*) = #Listlen(arguments.criteriaIDs)#

    sofar no problem and everything works fine. Now I want to offer the possibility for the user to add some AND and OR variables in their search, ie. someone could say: I'm looking for a person that possesses:

    Criteria 1 AND 3 AND 4 (which would be covered by the query above) AND (5 OR 6 OR 7) AND (8 OR 9) etc....

    I'm not sure where to start with this additional level.
    I hope someone else does..:)
    thanks!
    Bart
  2. my_s_e_l_f New Member

    select *
    from <table> a,
    <table> b
    where
    (condition1 = 'whatever' OR 1<>:variable_to_indicate_you_need_this_con1 )
    AND
    (condition2 = 'whatever' OR 2<>:variable_to_indicate_you_need_this_con2 )
    AND
    (condition2 = 'whatever' OR 3<>:variable_to_indicate_you_need_this_con3 )

    Pass 1 for :variable_to_indicate_you_need_this_con1 to "activate" condition1. 2 to :variable_to_indicate_you_need_this_con2 to "activate" condition2..so on...

    Cheers,
    Me

Share This Page