Skip to main content
Inspiring
September 21, 2007
Answered

help with an advance mysql query

  • September 21, 2007
  • 1 reply
  • 315 views
I'm using CF 7 to query a MySQL 5 DB.

I was wondering if it's possible to add an OR operator in a subquery or at the end of a conditional statment. Let's say that in my conditional WHERE statement after a few inclusive conditions, I wanted to add an "AND & OR" logic.
maybe by including part of my code I can be a little more clear:

<cfquery name="domysearch" ...>
SELECT
a.id as id,
.....
b.company as company,
.....
c.add_line1 as add_line1,
.....
FROM registratn_contacts a, registratn b, registratn_addresses c
WHERE a.owner = #arguments.owner#
AND b.id = a.master_record
AND c.id = a.address_id
AND c.state = 'NY'
OR c.state = 'CA'

I would like to know if I can or should accomplish this in my query or if I should have this logic outside the query?

Thanks for any help!!!!

Carlos
    This topic has been closed for replies.
    Correct answer paross1
    WHERE .....
    AND (c.state = 'NY' OR c.state = 'CA')

    You can use an OR, but it is probably better to use an IN clause, as it is essentially an OR, and would be more efficient.

    AND c.state IN ( 'NY', 'CA')

    Phil

    1 reply

    paross1Correct answer
    Participating Frequently
    September 21, 2007
    WHERE .....
    AND (c.state = 'NY' OR c.state = 'CA')

    You can use an OR, but it is probably better to use an IN clause, as it is essentially an OR, and would be more efficient.

    AND c.state IN ( 'NY', 'CA')

    Phil
    Inspiring
    October 1, 2007
    Thanks for the help, this information was very very helpful!!!

    Carlos