Sigit Dwi Prasetyo
Sigit Dwi Prasetyo

Reputation: 27

Using an array in SELECT IN query?

I want to know if it is possible to create a query where the WHERE clause has one condition but the value is an array.

This is the code & image related table in the database:

enter image description here

//array in user.id_lokasi => (PL001,PL002,PL003)    
SELECT * FROM `lokasi` 
         WHERE `id_lokasi` IN (SELECT id_lokasi FROM user WHERE id_user='admin')

Upvotes: 1

Views: 192

Answers (1)

lloiacono
lloiacono

Reputation: 5050

Your query above should work already. Regarding your question if you have a set of id_lokasi you want to use in the WHERE you can do this:

SELECT * FROM `lokasi` 
     WHERE `id_lokasi` IN ('PL001', 'PL002', 'PL003');

As to your original query, you could rewrite it like this to avoid the subquery:

SELECT l.* FROM `lokasi` l
LEFT JOIN user u ON
    u.id_lokasi = l.id_locasi AND u.id_user = 'admin'

Upvotes: 2

Related Questions