Search code examples
postgresqlhstore

Postgres / hstore : how to get IDs of rows with certain key in their hstore field?


I'm querying a pgsql DB to find rows that have certain keys in an hstore field:

select DISTINCT
from (select id, exist(data, ‘exercise_quiz’) key_exists
  from user_tracking) x
where key_exists = true;

It works fine, but I need to print the IDs of the corresponding rows it returns. Can I do this with this command?


Solution

  • Use the operator hstore ? text (does hstore contain key?):

    select id
    from user_tracking
    where data ? 'exercise_quiz';