3

I want to select distinct values of Column A where Column B = any members of a set S.

Is there a way to do this directly in SQL without looping and then filtering afterwards in code?

Thanks

EDIT: The set S is a PHP array. Does this make a difference?

1
  • What flavor of mysql library in PHP are you using? mysql, mysqli, PDO, etc.? Commented Sep 30, 2011 at 7:11

2 Answers 2

6

Use the IN clause with a list of values, or a subquery (not sure if supported in MySql since I use Oracle). The match can be on more than one column.

SELECT column_a
  FROM mytable
 WHERE column_b IN (1, 2, 3)

SELECT column_a
  FROM mytable
 WHERE column_b IN (SELECT column_c FROM myothertable)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is supported by MySQL.
2
SELECT DISTINCT
    columnA
FROM
    yourTable
WHERE
    columnB IN ( 'your', 'set', 'of', 'values' )

Comments

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.