0

I have an array of values that I want to check to see if it is in the database already. This array can grow upto 100 elements. What's the best way to check it? Should I pull all the table data and then check against it? Should/Can I do this in 1 SQL statement? If so, how? I'm using PHP.

Edit:

I should add that I am checking to see which ones are in the database so then I can add the ones that aren't in there. Maybe there's a better solution than what I was thinking.

2
  • What server side language are you using? Commented May 16, 2012 at 0:11
  • How silly of me to forget to mention! PHP Commented May 16, 2012 at 0:14

2 Answers 2

4

I am assuming PHP

$sql = "SELECT name FROM tablename WHERE name IN ('".implode("','", $array_of_names)."')";
Sign up to request clarification or add additional context in comments.

5 Comments

This returns the ones that are in the database. How can I have it tell me which ones aren't? Because I'm trying to insert only the new ones.
Use array_diff() to compare the two arrays. The ones remaining aren't in your database.
I think putting NOT in front of IN would just select all the rows where any of the ('...') values aren't in not in a row right? I wanted to know which ones in my array are not in the database already? or Am I misunderstanding something?
Thanks John Conde! Pronounced as Con-day?
Con-dee. Surprisingly some people think it's sounds like candy. I think they're brain dead. :)
1

Create a statement dynamically like this:

select *
from table
where value in (3, 5, 8, 12, 17, (etc))

produce the list of values from the array in whatever language you are using.

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.