Skip to content
New issue

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

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

Already on GitHub? # to your account

使用 whereLT decimal 类型列的问题 #3915

Closed
Weishan-Allen opened this issue Nov 12, 2024 · 13 comments · Fixed by #3997
Closed

使用 whereLT decimal 类型列的问题 #3915

Weishan-Allen opened this issue Nov 12, 2024 · 13 comments · Fixed by #3997
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@Weishan-Allen
Copy link

Go version

1.23.2

GoFrame version

2.7.4

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

使用

var v []*entity.V
WhereLT(dao.x.Columns().a, gdb.Raw(dao.x.Columns().b)).Scan(&v)

数据库 a,b 均为 decimal 类型 且 a为0.00 并且b 是大于0.00的任意数
且表只有一条记录(应该关系不大)

What did you see happen?

row 返回为0条记录

What did you expect to see?

row 返回为1

如果采用WhereGT 则可以正常返回
var v []*entity.V
WhereGT(dao.x.Columns().b, gdb.Raw(dao.x.Columns().a)).Scan(&v)

数据库 a,b 均为 decimal 类型 且 a为0.00 并且b 是大于0.00的任意数
猜测应该是和 0值有关

@Weishan-Allen Weishan-Allen added the bug It is confirmed a bug, but don't worry, we'll handle it. label Nov 12, 2024
@gqcn
Copy link
Member

gqcn commented Nov 14, 2024

@Weishan-Allen 请给定可复现该问题的最小可运行代码。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@Weishan-Allen Please provide the smallest runnable code that can reproduce the problem.

@wwwfeng
Copy link
Contributor

wwwfeng commented Nov 18, 2024

应该不是decimal的问题 WhereLT(col,val) 第二个参数是value 实际执行会变成一个值,然而你要求比较的是字段
应该使用Where(gdb.Raw("反引号a反引号<反引号b反引号"))Where(gdb.Raw("a<b"))

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It should not be a problem with decimal. WhereLT(col,val) The second parameter is value. In actual execution, it will become a value. However, what you want to compare is a field.
You should use Where(gdb.Raw("a<b")) or Where(gdb.Raw("a<b"))

@Weishan-Allen
Copy link
Author

@gqcn @wwwfeng 我修改了值证明了 确实如 @wwwfeng 所说一致,但打印出来sql非常有迷惑性。

type ScanTest struct {
	a float64
	b float64
}

func Test(ctx context.Context) {
	var scanTest *ScanTest
	err := dao.Test.Ctx(ctx).WhereLT("a", gdb.Raw("`b`")).Scan(&scanTest)
	if err != nil {
		return
	}
	// 日志:  [rows:0  ] SELECT `a`,`b` FROM `test` WHERE `a` < `b` LIMIT 1

	err = dao.Test.Ctx(ctx).WhereGT("b", gdb.Raw("`a`")).Scan(&scanTest)
	if err != nil {
		return
	}

	// 日志: [rows:1  ] SELECT `a`,`b` FROM `hg_test` WHERE `b` > `a` LIMIT 1
}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@gqcn @wwwfeng I modified the value to prove that it is indeed consistent as @wwwfeng said, but the printed sql is very confusing.

typeScanTest struct {
a float64
b float64
}

func Test(ctx context.Context) {
var scanTest *ScanTest
err := dao.Test.Ctx(ctx).WhereLT("a", gdb.Raw("`b`")).Scan(&scanTest)
if err != nil {
return
}
// Log: [rows:0 ] SELECT `a`,`b` FROM `test` WHERE `a` < `b` LIMIT 1

err = dao.Test.Ctx(ctx).WhereGT("b", gdb.Raw("`a`")).Scan(&scanTest)
if err != nil {
return
}

// Log: [rows:1 ] SELECT `a`,`b` FROM `hg_test` WHERE `b` > `a` LIMIT 1
}

@Weishan-Allen
Copy link
Author

这让我以为它正常的工作了,所以WhereGT or LT 不支持第二个参数使用gdb.Raw吗

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This makes me think it is working normally, so WhereGT or LT does not support the second parameter using gdb.Raw?

@gqcn
Copy link
Member

gqcn commented Nov 23, 2024

这让我以为它正常的工作了,所以WhereGT or LT 不支持第二个参数使用gdb.Raw吗

支持呀!你看你之前发的结果。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This makes me think it is working normally, so WhereGT or LT does not support the second parameter using gdb.Raw?

Support it! Look at the results you posted before.

@Weishan-Allen
Copy link
Author

这让我以为它能正常工作了,所以 GT 或 LT 不支持使用 gdb.Raw 的个参数吗?

支持呀!你看你之前得到的结果。

@gqcn 抱歉最近有点忙,我现在新开了一个项目 数据库表结构以及数据如下
image

image

很明显 a > b 的记录 和 b > a 的记录各一条

现在我写了个示例去查询 a < b 和 a > b 的例子

type ScanTest struct {
	a float64
	b float64
}
func Test(ctx context.Context) {
	var LTTest = make([]ScanTest, 0)
	err := dao.WsTest.Ctx(ctx).WhereLT("a", gdb.Raw("`b`")).Scan(&LTTest)
	if err != nil {
		return err
	}

	var GTTest = make([]ScanTest, 0)
	err = dao.WsTest.Ctx(ctx).WhereGT("a", gdb.Raw("`b`")).Scan(&GTTest)
	if err != nil {
		return err
	}

	return nil
}

打印的日志:

2024-11-27 15:45:02.031 [DEBU] {200500558fc30b18c3c2490d602ca9fb} [  3 ms] [default] [toy] [rows:1  ] SHOW TABLES
2024-11-27 15:45:02.032 [DEBU] {200500558fc30b18c3c2490d602ca9fb} [  0 ms] [default] [toy] [rows:3  ] SHOW FULL COLUMNS FROM `ws_test`
2024-11-27 15:45:02.033 [DEBU] {200500558fc30b18c3c2490d602ca9fb} [  1 ms] [default] [toy] [rows:0  ] SELECT `a`,`b` FROM `ws_test` WHERE `a` < `b`
2024-11-27 15:45:02.033 [DEBU] {200500558fc30b18c3c2490d602ca9fb} [  0 ms] [default] [toy] [rows:2  ] SELECT `a`,`b` FROM `ws_test` WHERE `a` > `b`

这似乎并没有工作

@Weishan-Allen Weishan-Allen reopened this Nov 27, 2024
@gqcn gqcn self-assigned this Dec 1, 2024
@gqcn
Copy link
Member

gqcn commented Dec 1, 2024

@Weishan-Allen 好的,我看看。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@Weishan-Allen Okay, let me see.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
4 participants