passing list to IN clause in HQL or SQL?

from AUTOS a where a.model in (select m.model from MODELS m) or Query query1 = session.createQuery(“select s.id from Salary s where s.salary < 50000 AND s.salary > 49980”); Query query2 = session.createQuery(“from Employee e where e.id in (:ids)”).setParameterList(“ids”, query1.list()); query2.list();

What is the best approach using JDBC for parameterizing an IN clause? [duplicate]

There’s indeed no straightforward way to do this in JDBC. Some JDBC drivers seem to support PreparedStatement#setArray() on the IN clause. I am only not sure which ones that are. You could just use a helper method with String#join() and Collections#nCopies() to generate the placeholders for IN clause and another helper method to set all … Read more

JPA passing list to IN clause in named native query

The above accepted answer is not correct and led me off track for many days !! JPA and Hibernate both accept collections in native query using Query. You just need to do String nativeQuery = “Select * from A where name in :names”; //use (:names) for older versions of hibernate Query q = em.createNativeQuery(nativeQuery); q.setParameter(“names”, … Read more

MySQL variable format for a “NOT IN” list of values

You can’t use the IN clause like that. It compiles to a single string in your IN clause. But an IN clause needs separate values. WHERE id_campo not in (@idcamposexcluidos) compiles to WHERE id_campo not in (‘817,803,495’) but it should be WHERE id_campo not in (‘817′,’803′,’495’) To overcome this either use dynamic SQL or in … Read more

PHP – Using PDO with IN clause array

PDO is not good with such things. You need to create a string with placeholders dynamically and insert it into the query, while binding array values the usual way. With positional placeholders it would be like this: $in = str_repeat(‘?,’, count($in_array) – 1) . ‘?’; $sql = “SELECT * FROM my_table WHERE my_value IN ($in)”; … Read more