How to create a placeholder part to use in the IN clause

Introduction

There are times when you want to dynamically create the conditional part of SQL. Specifically, when the value set in the IN clause changes dynamically.

SELECT foo FROM sample_table
WHERE bar IN (?, ?, ?, ?, ?); --Here?I want to deal with when the number of

Such SQL is prone to performance issues, but it's often acceptable for small projects. I have an image of a problem that comes when I forget it, and I can write it by using the for statement honestly, but I'd like to write it concisely.

Java implementation

int size = 10; //I want to create?Number of
String placeholders = IntStream.range(0, size)
                               .boxed()
                               .map(i -> "?")
                               .collect(Collectors.joining(", ", "(", ")"));
System.out.println(placeholders);
// (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

in conclusion

Personally, if I find an innovative writing style (in each language), I would like to add it.

Recommended Posts

How to create a placeholder part to use in the IN clause
How to create a theme in Liferay 7 / DXP
How to easily create a pull-down in Rails
How to create a method
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to use the link_to method
How to use Lombok in Spring
How to use the getter / setter method (in object orientation)
How to use the include? method
How to use the form_with method
How to use the wrapper class
How to create a form to select a date from the calendar
[Java] How to create a folder
Create a method to return the tax rate in Java
How to use InjectorHolder in OpenAM
How to add the same Indexes in a nested array
How to use classes in Java?
How to create a service builder portlet in Liferay 7 / DXP
[Enum_help] Use enum_help to create a select box displayed in Japanese!
How to create a jar file or war file using the jar command
How to create a registration / update function where the table crosses
How to create a new Gradle + Java + Jar project in Intellij 2016.03
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to store the information entered in textarea in a variable in the method
A memorandum on how to use Eclipse
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
How to use custom helpers in rails
[Java] How to use the hasNext function
How to use named volume in docker-compose.yml
How to insert a video in Rails
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
How to create a Maven repository for 2020
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
[Processing × Java] How to use the class
How to get the date in java
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Processing × Java] How to use the function
How to use environment variables in RubyOnRails
[Java] How to use the Calendar class
How to publish a library in jCenter
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use ExpandableListView in Android Studio
How to get the current date as a string in yyyyMMdd format
[Java] How to use substring to cut out a part of a character string
How to change the value of a variable at a breakpoint in intelliJ
I thought about the best way to create a ValueObject in Ruby
How to get the absolute path of a directory running in Java
[Rails] How to create a table, add a column, and change the column type
How to reference a column when overriding the column name method in ActiveRecord
How to create your own annotation in Java and get the value
How to create a database for H2 Database anywhere
How to use the camera module OV7725 (ESP32-WROVER-B)