Skip to main content
February 12, 2014
Question

IN statment

  • February 12, 2014
  • 3 replies
  • 1562 views

I have the table:

   

IDcondition_id
property_id
1c_3b_1
2c_13b_2
3c_3b_4
4c_13b_1
5c_3b_2
6c_4b_1

I need to select all properties with condition_id c_3 and c_13 not just c_3 or c_13 (I expect in the result it should be only b_1 and b_2)

The query like this:

SELECT *

FROM table

WHERE condition_id IN (  3,13                                                            )

The result (b_1, b_2, b_4)

What do I do wrong?

This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
February 20, 2014

Sergey_S wrote:

I have the table:

   

IDcondition_id
property_id
1c_3b_1
2c_13b_2
3c_3b_4
4c_13b_1
5c_3b_2
6c_4b_1

I need to select all properties with condition_id c_3 and c_13 not just c_3 or c_13 (I expect in the result it should be only b_1 and b_2)

The query like this:

SELECT *

FROM table

WHERE condition_id IN (  3,13                                                            )

A simple way to do it is:

SELECT *

FROM table

WHERE condition_id IN ('c_3') AND property_id IN (SELECT property_id FROM table WHERE condition_id IN ('c_13'))

Fernis
Inspiring
February 12, 2014

Your question (or English) was a bit vague, but here goes:

If you want just c_3 and c_13's returned

SELECT id,condition_id,property_id

FROM table

WHERE condition_id = 'c_3' OR condition_id = 'c_13'

But it seemed you wanted to filter out the results further by having only c_3 and c_13's which also match b_1 or b_2

SELECT id,condition_id,property_id

FROM table

WHERE (condition_id = 'c_3' OR condition_id = 'c_13')

AND (property_id = 'b_1' OR property_id = 'b_2')

Alternatively,

SELECT id,condition_id,property_id

FROM table

WHERE condition_id IN ('c_3,'c_13')

AND property_id IN ('b_1','b_2')

-Fernis

Inspiring
February 12, 2014

Can you show us your exact cfquery statement?  Hard to believe those results!  The actual syntax you want would be:

SELECT *

FROM table

WHERE condition_id='c_3' AND condition_ID='c_13')

The IN operator is just a shortcut for a bunch of OR operators.

hth

Reed

February 12, 2014

Thank you REEDPOWELL

I did try this:

SELECT *

FROM table

WHERE

                                                            <cfloop list="#condition_id#" index="iii">

                                                             condition_id=#iii#<cfif iii is "#trim(ListLast(condition_id))#"><cfelse> and </cfif>

                                                            </cfloop>

Or to make clear, exactly as you are suggesting this:

SELECT *

FROM table

WHERE condition_id='c_3' and condition_id='c_13'

But return is 0 records.

Inspiring
February 12, 2014

Two things:  One, the CFLOOP code is not equivalent to the code I had written. I cannot honestly tell you exactly what that the CFLOOP code is producing.

But the 2nd bit of code you showed should work, so I have to ask at this point - have you tried executing this code in SSMS, and does it work there?  There is something else going on here, probably with the values in your table.  Are those varchar() columns or char() columns?  Are there trailing spaces that need to be trimmed during the compare operations?  First step is to verify that it works in SSMS before trying to find the CF code bug.

-reed