简体   繁体   中英

Querying inside Postgresql hstore

I have a products table which has many variants, variants table has a price column with hstore datatype.

I have two queries

Query 1

SELECT variants.* FROM variants WHERE (CAST(variants.price -> 'sg' AS INT) > 1000)

Query 2

SELECT products.* FROM products INNER JOIN variants ON variants.checkoutable_id = products.id AND variants.checkoutable_type = 'Product' WHERE (CAST(variants.price -> 'sg' AS INT) > 1000)

While the first query fails with an error message ERROR: invalid input syntax for integer: "not a valid number" the second query works perfectly fine.

Building off of my comment, let's figure out how to find the problematic data. I'm going to assume you have an overwhelming number of rows in the variants table -- enough rows that manually looking for non-numeric values is going to be difficult.

First, let's isolate the rows which are not covered by the second query.

SELECT *
FROM variants
WHERE
  checkoutable_type != 'Product' OR
  checkoutable_id NOT IN (SELECT id FROM products);

That will probably take a while to run, and just be a big data dump. We're really interested in just price->'sg' , and specifically the ones where price->'sg' isn't a string representation of an integer.

SELECT price->'sg'
FROM variants
WHERE
  (checkoutable_type != 'Product' OR
   checkoutable_id NOT IN (SELECT id FROM products)) AND
  price->'sg' !~ '[0-9]';

That should list out the items not joined in, and which include non-numbers in the string. Clean those up, and your first query should work.

One or more rows of variants have improper content for integer, namely "not a valid number" . Run the query to check which ones:

select *
from variants
where price->'sg' like 'not%';

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM