Cunning
Cunning

Reputation: 55

How Can I Write an SQL Query for the Contents of a Cell Array?

Apologies if this should be fairly obvious! If I have a cell array of keys - how should I best query these against a database?

For example if I have the cell array:

Names = { 'Jon', 'Peter', 'Paul' };

Do I have to write the SQL in the form:

select *
from x
where name = 'Jon' or name = 'Peter' or name = 'Paul';

Or is there some way of writing it of the form:

select *
from x
where name = {Names};

Whilst I can write a function to generate the where clause, this feels far from ideal!

Any help would be greatly appreciated.

Upvotes: 1

Views: 508

Answers (2)

jpsfer
jpsfer

Reputation: 594

I can think of at least 2 ways:

In the where clause you could use: "in ('Jon','Peter')"

Or if you have the names you want to query in a table just use "where names in (Select Name from [table with names])"

Hope that helps.

Upvotes: 1

John Woo
John Woo

Reputation: 263723

You can use IN

SELECT *
FROM   x
WHERE  name IN ('Jon', 'Peter', 'Paul')

Upvotes: 2

Related Questions