Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么在查询时field支持自定义sql #900

Open
SummerXZX opened this issue Jun 29, 2023 · 7 comments
Open

怎么在查询时field支持自定义sql #900

SummerXZX opened this issue Jun 29, 2023 · 7 comments
Assignees

Comments

@SummerXZX
Copy link

SummerXZX commented Jun 29, 2023

比如我有一张user表,表中有两个字段:id, status (pending, success ,failed),现在我需要一个sql查询出各个状态的数量
原生sql:
select
sum(if (u.status = 'pending' , 1, 0)) pending_count,
sum(if (u.status = 'success' , 1, 0)) pending_count,
sum(if (u.status = 'failed' , 1, 0)) pending_count
from user u
怎么使用gen实现呢

@qqxhb
Copy link
Member

qqxhb commented Jun 29, 2023

@RiverChu 看下文档的 dynamic sql
或者你这个 是不是直接 count+group 也可以,业务逻辑转下

@SummerXZX
Copy link
Author

gen.Field,能增加一个支持自定义sql的类型吗,现在的field转成sql都是 tableName.field,但是有时候我们的field并不是完全都是这种格式,比如我上面的那种

@SummerXZX
Copy link
Author

func NewRaw(ex clause.Expr) Field {
return Field{expr: expr{
e: ex,
}}
}

可不可以给field增加一个这样的方法,支持自定义sql

@MikyChow
Copy link

我也遇到了同样的问题,比如我表里有两个字段x,y,我需要查询x<y+1的,这时候也需要动态的。
我看了下源码,field不管怎么建都会走到toColumn中,然后banColumnRaw会强制raw=fale,导致必须要有‘,从而导致报错

@hyqc
Copy link

hyqc commented Oct 20, 2023

比如我有一张user表,表中有两个字段:id, status (pending, success ,failed),现在我需要一个sql查询出各个状态的数量 原生sql: select sum(if (u.status = 'pending' , 1, 0)) pending_count, sum(if (u.status = 'success' , 1, 0)) pending_count, sum(if (u.status = 'failed' , 1, 0)) pending_count from user u 怎么使用gen实现呢

同问!

@MikyChow
Copy link

MikyChow commented Oct 25, 2023

比如我有一张user表,表中有两个字段:id, status (pending, success ,failed),现在我需要一个sql查询出各个状态的数量 原生sql: select sum(if (u.status = 'pending' , 1, 0)) pending_count, sum(if (u.status = 'success' , 1, 0)) pending_count, sum(if (u.status = 'failed' , 1, 0)) pending_count from user u 怎么使用gen实现呢

同问!

This PR can help you to build customer raw field
#968

@luxun9527
Copy link

如果想在select或者where的时候自定义可以参考一下我 的一个做法,使用ast修改gen生成的代码,使生成的代码增加可以自定义select或者where的方法具体可以参考,对您有帮助的话帮我点一个star就是对我的鼓励
https://github.com/luxun9527/go-lib/blob/master/utils/ast_apply/add_rawselect_rawmethod.go

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

No branches or pull requests

6 participants