Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lindaifeng
Copy link
Contributor

在实际业务场景中存在关联字段类型不一致的情况,目前的代码在left join关联时发现无法兼容字段类型不一致的情况,需要在sql中拼接cast函数进行转换

如:pg数据库A表id为int、B表id_str为varchar、C表id为int

打印出来的sql为:
SELECT "A".*, "B".*, "C".* FROM "public"."A" AS "A" LEFT JOIN ( SELECT * FROM "public"."B" ) AS "B" ON "B"."id_str" = "A"."id" LEFT JOIN ( SELECT * FROM "public"."C" ) AS "C" ON "C"."id" = "B"."id_str" LIMIT 20000

导致出现类型不匹配的错误
image

修改代码兼容cast函数后

打印出来的sql为:
SELECT "A".*, "B".*, "C".* FROM "public"."A" AS "A" LEFT JOIN ( SELECT * FROM "public"."B" ) AS "B" ON CAST("B"."id_str" AS integer) = "A"."id" LEFT JOIN ( SELECT * FROM "public"."C" ) AS "C" ON CAST("C"."id" AS varchar) = "B"."id_str" LIMIT 20000

数据响应成功
image

@TommyLemon TommyLemon merged commit 5e2ed07 into Tencent:master Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants