algorithmicCoder
algorithmicCoder

Reputation: 6799

SQL query to match multiple values to a field in select statement

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?

Upvotes: 3

Views: 8845

Answers (2)

Iain
Iain

Reputation: 76

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)

Upvotes: 6

Decent Dabbler
Decent Dabbler

Reputation: 22793

SELECT DISTINCT
    columnA
FROM
    yourTable
WHERE
    columnB IN ( 'your', 'set', 'of', 'values' )

Upvotes: 2

Related Questions