0

I have a table with columns: conditional1, conditional2, data. I need to return all the rows where conditional1 = value1 if any of those rows also contains conditional2 = value2. How can I do this?

Edit: There's some confusion about what I'm asking.

If you have columns

`conditional1 | conditional2 | data

A | A | A

A | B | A`

I want to return both rows if conditional1 = A and conditional2 = B

Edit 2: There's still some confusion.

Here is another example:

conditional1 | conditional2 | data

1 | 1 | A

1 | 2 | B

2 | 1 | C

2 | 2 | D

If conditional1 = 1 and conditional2 = 1, it should return

1 | 1 | A

1 | 2 | B

If conditional1 = 2 and conditional2 = 1, it should return

2 | 1 | C

2 | 2 | D

If conditional1 = 2 and conditional2 = 2, it should return

2 | 1 | C

2 | 2 | D

If conditional1 = 2 and conditional2 = 3, it should return no rows.

8
  • Are you stuck with the query itself, or implementing this in PHP, or ? Commented Apr 2, 2011 at 10:41
  • I edited the question because there was some confusion. Commented Apr 2, 2011 at 10:50
  • You question still makes no sense. (Perhaps that's why all of the existing answers are incorrect.) Do you mean if conditional1 = A OR conditional2 = B? Commented Apr 2, 2011 at 10:51
  • you didn't clarified. mark baker answered this question as it is Commented Apr 2, 2011 at 10:51
  • What condition determines whether you return the first row? With your edit, this is very unclear... what links the first row to the second to say that it should be returned even though conditional2 is not 'B'? Commented Apr 2, 2011 at 10:53

2 Answers 2

3
select *
from tbl A
where conditional1 = 'A'
and exists (
  select * from tbl B
  where B.conditional1 = 'A' and B.conditional2 = 'B')

or a more MySQL friendly version

select * from tbl
where conditional1 in
(
    select conditional1 
    from tbl
    where conditional1 = 'A' and conditional2 = 'B'
)
Sign up to request clarification or add additional context in comments.

7 Comments

I tried your second version, but I'm getting syntax errors with it:select * from table where conditional1 in ( select conditional1 from table where conditional1 = 1 and conditional2 = 1 )
If you're using the literal "table", then that's obviously wrong. Otherwise I syntax checked in MySQL and SQL Server at least, both are fine with it
You're right -- using "table" in the example was not the smartest thing to do. Your answer works great. Thanks!
Ah I see. I think = and DISTINCT still might perform better than IN from this OP's experience?
@Martin / ok got you now. I should have used '='. But this is a very simple query, so the bug won't bite? I tend to leave unedited answers alone, since it looks .. clean :)
|
0
SELECT * FROM table 
    WHERE conditional1 = 'A' AND 1 IN 
        ( SELECT 1 FROM table WHERE conditional1 = 'A' AND conditional2 = 'B' LIMIT 1 )

I think I finally understand!

1 Comment

Sorry, there's a problem on my end: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.