繁体   English   中英

Laravel / Postgres数据库查询?

[英]Laravel/Postgres db queries?

我对Laravel / Postgres DB Queries的查询有(奇怪)问题。 我将在此处粘贴整个查询(虽然有点太长,但我们只关注其中一部分):

select distinct "products".*, "brands"."name" as "brand", "brands"."image" as "brand_image", "product_categories"."name" as "category", "product_categories"."slug" as "category_slug", count(distinct product_features.feature_category_id) as features_count from "products" inner join "brands" on "products"."brand_id" = "brands"."id" inner join "product_categories" on "products"."category_id" = "product_categories"."id" left join "product_features" on "products"."id" = "product_features"."product_id" where "products"."deleted_at" is null and "products"."category_id" in (142) and "products"."published" = true and "brands"."published" = true and "product_categories"."published" = true and "product_categories"."deleted_at" is null and "price" >= 0 and "price" <= 19000 and "products"."brand_id" in (17) and 
            (product_features.feature_category_id = 555 and
            CAST(product_features.value AS BOOLEAN) = true and
            product_features.deleted_at IS NULL) or
            (product_features.feature_category_id = 554 and
            CAST(product_features.value AS BOOLEAN) = true and
            product_features.deleted_at IS NULL) group by "products"."id", "brands"."name", "brands"."image", "product_categories"."name", "product_categories"."slug" having count(distinct product_features.feature_category_id) = 2 order by price asc

这是初始查询,但是当我执行它时,出现奇怪的错误:

ERROR:  invalid input syntax for type boolean: "800"

我不知道为什么会引发此错误?

您的product_features.value是一个文本字段。 虽然Postgres可以将所有非零整数(例如800强制转换为boolean true ,但不能将'800'这样'800' text强制转换为boolean

解决方案是将空和/或相等性上'0' product_features.value检查为'0'而不是CAST(product_features.value AS BOOLEAN) = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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