1

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')
3
  • 1
    Yes, Your query is correct! Commented Feb 20, 2018 at 8:08
  • If i type manually SELECT * FROM lokasi WHERE id_lokasi IN ('PL001', 'PL002', 'PL003'); its correct, but when i select from table user there is no data show Commented Feb 20, 2018 at 8:26
  • 2
    You need to normalize your database, you should not have a list of keys in a single database field. Commented Feb 20, 2018 at 8:33

1 Answer 1

2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

when I change the parameters in u.id_user='user', all data appears... I just want to bring up the data based on array id_lokasi in user table.

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.