I have a table orders with a field order_detail of type hstore which saves json data.
Now I want to query on the inner objects of my json data. because The query is somehow complicated I'm trying to tell my problem in easier scenario.
I have tested these 4 subqueries:
Get the original saved json:
select order_detail::json as original from orders;
This query successfully returns the json formatted data.
Get the inner object 'transaction' inside the order_details:
select order_detail::json as original, (order_detail -> 'transaction')::json as transaction from order_details;
This query also works successfully.
Get the id of that transaction:
select order_detail::json as original, (order_detail -> 'transaction')::json as transaction, ((order_detail -> 'transaction')::json -> 'id')::text as id from order_details;
The above also works successfully and returns the original json, transaction and id inside the transaction.
Select based on the result of query 3 and get one of the results:
select original from (select order_detail::json as original, (order_detail -> 'transaction')::json as transaction, ((order_detail -> 'transaction')::json -> 'id')::text as id from order_details) s where transaction is null and id is null;
This query will raise an exception: The exception says that:
[22P02] ERROR there is a token '=' is invalid**
Why does this exception occurs only in the 4th query? Can anyone help me on this?
Finally I found the problem. When you want have such a field which is json stored in hstore, if you use a simple query you can use something like this:
order_detail -> 'trasaction'
and this will return the transaction part of hashed-stored data in the order_detail field with no problem.
BUT if you want to use such thing in a nested query, you MUST explicitly declare that the field is json. so you must use something like this instead (in all parts of inner query):
order_detail::json -> 'transaction'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.