1

I have an array of ids, I have to select from table for each value of array , i can get it by one by one in for loop,

SELECT  point, privacy FROM `tableName` WHERE id='1403176452487620892'and status=1

but the problem is that array size is 100, i need a single query not 100.

5
  • What language is this coming from? Java? Commented Jun 20, 2014 at 6:46
  • @AlvinThompson i am using Scala Commented Jun 20, 2014 at 6:52
  • Yep, loop and bind the values through the JDBC driver. Commented Jun 20, 2014 at 6:55
  • Scala on the JVM, right? Commented Jun 20, 2014 at 6:56
  • Not that similar, but you're running it on the JVM so you're using the JDBC driver. That means you can bind the values no problem. Commented Jun 20, 2014 at 7:07

3 Answers 3

2

Why can't you use:

SELECT  point, privacy FROM `tableName` WHERE status=1 and id in(?,?,?...)

Yes, it's ridiculously long, but if one query is what you need...

Sign up to request clarification or add additional context in comments.

5 Comments

Use a for loop for that as well your comment in that post, sorry i didn't get it
@GovindSinghNagarkoti: but now you do?
@GovindSinghNagarkoti: sorry, what did you mean by, "means its not a correct way to use IN my case"?
did i have to use your query or not
Yep. No alternative in JDBC.
1

You can use like this :

   $ids = join(',',$ids);  
   $sql = "SELECT * FROM tableName WHERE id IN ($ids)";

11 Comments

Is the array of IDs in the DB? MySQL can't do for loops, can it?
$ids = join(',',$ids); not familiar with this syntax did it convert it to comma delimated string
Yes,it convert it to comma delimated string.
@AlvinThompson its not in db
@GovindSinghNagarkoti: so use a for loop in whatever language it's coming from to construct that huge in statement with all those values, and bind the values from the array. Unless you're using Oracle, having 100 values in your in statement presents no problem.
|
0

You can try like this

$array = array(1,2,3);
$str = implode(",", $array);
$sql = "SELECT  point, privacy FROM `tableName` WHERE id in ($str) and status=1";

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.