1

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.

Cunning
  • 55
  • 1
  • 1
  • 8

2 Answers2

2

You can use IN

SELECT *
FROM   x
WHERE  name IN ('Jon', 'Peter', 'Paul')
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Can in take a cell array rather than a string? I want to generate the queries based on they contents of the array (which are not constant). – Cunning Aug 24 '12 at 09:21
  • read this, [*FIND_IN_SET*](http://stackoverflow.com/questions/4155873/find-in-set-vs-in) – John Woo Aug 24 '12 at 09:22
1

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.

jpsfer
  • 594
  • 3
  • 7
  • 18