0

I'm interested in creating a query of a raw json blob without having to use a subquery in PostgreSQL. The query looks like this:

 SELECT id, 
    ((db_bench_results.result::json -> 'Runner'::text)) as runner
   FROM public.db_bench_results as full_results 
   WHERE ((db_bench_results.result::json -> 'Runner'::text)) = "this_runner";

However, when I do this, I get:

ERROR:  column "this_runner" does not exist
LINE 11: ...E ((db_bench_results.result::json -> 'Runner'::text)) = "this_runner";

Is this possible without creating a subquery and joining the tables together?

1
  • 3
    text in postgres uses single quotes ', double quotes " are reserved for named fields, so just change the condition to = 'this_runner' Commented Jan 19, 2022 at 20:46

2 Answers 2

3

String constants have to be enclosed in single quotes in SQL. The double quotes are for identifiers.

As you want to compare a value as text, use the ->> operator to make the returned value a text not a jsonb (or json) value.

So the following should work:

SELECT id, 
       db_bench_results.result ->> 'Runner' as runner
FROM public.db_bench_results as full_results 
WHERE db_bench_results.result ->> 'Runner' = 'this_runner';

This assumes that results is a jsonb (which it should be) or json column.

Sign up to request clarification or add additional context in comments.

Comments

0

Ended up doing this, is this efficient?

 SELECT f.runner from (select ((db_bench_results.result -> 'Runner'::text))::text AS runner,
   FROM public.db_bench_results) as f where f.runner like '%this_runner%'

Comments

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.