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

database/gdb: Problem with subquery usage #3922

Open
chaegumi opened this issue Nov 14, 2024 · 1 comment
Open

database/gdb: Problem with subquery usage #3922

chaegumi opened this issue Nov 14, 2024 · 1 comment
Assignees
Labels

Comments

@chaegumi
Copy link

chaegumi commented Nov 14, 2024

What do you want to ask?

package main

import (
	"context"
	"time"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

func getData(ctx context.Context) error {
	curtime := time.Now()
	curdate := curtime.Format("2006-01-02")

	startTime := curdate + " 00:00:00"
	endTime := curdate + " 23:59:59"

	var results []struct {
		Id      int64
		SiteId  int64
		Keyword string
		Domain  string
	}

	subQuery := g.Model("serp_keywords_cron B").Fields("B.id").Where("B.keyword_id", "A.id").Where("B.created_time>=", startTime).Where("B.created_time<=", endTime)

	return g.Model("serp_keywords A").Where("NOT EXISTS ?", subQuery).Limit(1).Scan(&results)
}

func main() {

	ctx := gctx.New()

	sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
		return getData(ctx)
	})
	if err != nil {
		panic(err)
	}

	g.Dump(sql)

}

解析出来的sql有问题,是我写法错误吗?

SELECT `id`,`site_id`,`keyword` FROM `serp_keywords` A WHERE NOT EXISTS (SELECT B.id FROM `serp_keywords_cron` B WHERE (`B`.`keyword_id`=**'A.id'**) AND (B.created_time>='2024-11-14 00:00:00') AND (B.created_time<='2024-11-14 23:59:59')) LIMIT 1

B.keyword_id='A.id'这个解析不能正确处理

@Issues-translate-bot Issues-translate-bot changed the title database/gdb: 子查询用法的问题 database/gdb: Problem with subquery usage Nov 14, 2024
@gqcn gqcn self-assigned this Nov 23, 2024
@github-actions github-actions bot removed the inactive label Nov 23, 2024
@gqcn
Copy link
Member

gqcn commented Nov 23, 2024

@chaegumi The second parameter of function Where is the value of the first parameter, like Where(key, value). You can use Where(gdb.Raw("B.keyword_id=A.id")) or Where("B.keyword_id", gdb.Raw("A.id")).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants