簡體   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