记录一次gorm查询无数据无报错

sql := "select user_id,add_time,page_name from pv_zsjpv_2022_5 where " +
		" add_time between ? and ? and user_id=? and product_key=? limit ?,?"

	var params []interface{}
	params = append(params, "2022-05-01 00:00:00")
	params = append(params, "2022-05-05 01:00:00")
	params = append(params, "8230004303622879")
	params = append(params, "chess")
	params = append(params, 1)
	params = append(params, 10)

	var data =make([]map[string]interface{},0)
	e:=db.Raw(sql,params...).Scan(&data).Error
	if e!=nil {
		fmt.Println("e=",e.Error())
	}else{
		fmt.Println(data)
	}

上面的sql语句是可以执行的,可以看看下面的sql语句,与上面一点点的变化

db := dbConns["wi_pv"]
sql := "select user_id,add_time,page_name from pv_zsjpv_2022_5 where " +
	" add_time between ? and ? and user_id=? and product_key=? limit ?,?"

var params []interface{}
var startTime,_=cast.StringToDate("2022-05-01 00:00:00")
var enTime,_=cast.StringToDate("2022-05-05 00:00:00")
params = append(params, startTime)
params = append(params, enTime)
params = append(params, "8230004303622879")
params = append(params, "chess")
params = append(params, 1)
params = append(params, 10)

var data =make([]map[string]interface{},0)
e:=db.Raw(sql,params...).Scan(&data).Error
if e!=nil {
	fmt.Println("e=",e.Error())
}else{
	fmt.Println(data)
}

对于interface类型,作为万能参数,但是遇到time类型时就不行了,这时候我试着打印执行语句,也没问题,就是没结果也没报错。换上面的第一种就可以了。

以下为sql打印日志

2022/05/16 18:45:45 D:/mywork/wiinterface_cqgd-local-zsj/area/app_start.go:197 SLOW SQL >= 50ms
[125.000ms] [rows:1] select count(*) from pv_zsjpv_2022_5 where  add_time between '2022-05-01 00:00:00' and '2022-05-05 01:00:00'   and user_id='8230004303622879' and product_key='chess'
2022-05-16 18:45:45.702 [info] [request] [status:200/0000] [exc_time:1924.00 ms] [middleware_logfile.go(wiweb.MiddlewareLogToFile):93] [route:/getPV]  >>> info={"content_type":"","method":"GET","render_type":"JSON","req_data":{"end_time":["2022-05-05 01:00:00"],"num":["10"],"page":["1"],"product":["chess"],"start_time":["2022-05-01 00:00:00"],"userid":["8230004303622879"]},"req_ip":"127.0.0.1","req_uri":"http://127.0.0.1:8111/getPV?product=chess&userid=8230004303622879&start_time=2022-05-01%2000:00:00&end_time=2022-05-05%2001:00:00&page=1&num=10"}

你可能感兴趣的:(mysql,Go语言,SQL查询,时间类型,数据库操作,接口)