Prince of Sweden Prince of Sweden - 4 years ago 131
Java Question

SQL: Select multiple rows with the same ID

I have a bit of a problem. I have a table with names, IDs and different geometry like so:

ID | Name| RealID| Geometry|
==========================================
1 | Hampshire | 3 | 0762453...
2 | Hampshire | 3 | 0156245...
3 | Salt Lake | 2 | 312455...
4 | Hampshire | 3 | 016422....


I have a need of selecting all Hampshire rows based on a list of IDs. As you can see my table has different geometries for say Hampshire.
I need all of them somehow by just comparing against one ID I get from a list of them.

I get the list of IDs from Java. It is simply a list with one ID(3) so far.

Doing this:

Select * from table where RealID = any(:listOfIds)


It only returns me one row no matter what if I send in Hampshire's ID in the list (3). I have tried something like this:

Select * from table where RealID IN (any(:listofids))


but the syntax is wrong and I'm not sure what to do to achieve my goal.

Answer Source

You can try the following syntax

SELECT * FROM table WHERE RealID in (listofids)

for example:

SELECT * FROM table WHERE RealID in (3,2,6....)

You can tr this also:

SELECT * FROM table WHERE FIND_IN_SET('3', RealID);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download