GORM Playground Link

https://github.com/go-gorm/playground/pull/1

Description

     自动会多出一个条件( `order`. = 0.000000  ),  使用 First 自动会多出一个order by 排序, 使用原生Db.Raw 也会多出这些, 这个是大BUG ?
  SELECT sum(1) AS order_sum, SUM(order.amount) as title_amount FROM `order` WHERE TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW()) AND `order`. = 0.000000 LIMIT 1

    SQL=========== {0 0} model value required

Comment From: jicvi

  db := global.Db.Table(m.table)                                                                                          
  sql := db.Select("sum(1) AS order_sum, SUM(order.amount) as title_amount")                                                                                    
  sql = sql.Where("TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW())")
  err := sql.Take(&counts.OrderCount, &counts.Amount).Error

Comment From: jicvi

  db := global.Db.Table(m.table)                                                                                          
  sql := db.Select("sum(1) AS order_sum, SUM(order.amount) as title_amount")                                                                                    
  sql = sql.Where("TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW())")
  err := sql.Take(&counts.OrderCount, &counts.Amount).Error

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jicvi

SELECT sum(1) AS order_sum, SUM(order.amount) as title_amount FROM order WHERE TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW()) AND order. = 0.000000 ORDER BY order. LIMIT 1

SQL=========== {0 0} model value required

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: alresvor

take 或者 find 之类的函数的第二个参数,都是内联条件,不是返回数据的 https://gorm.io/zh_CN/docs/query.html#内联条件 你应该用 Scan 返回数据 https://gorm.io/zh_CN/docs/sql_builder.html#Row-amp-Rows

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jicvi

@alresvor 使用Scan返回 sql: Scan error on column index 0, name "order_count": destination not a pointer

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: alresvor

@jicvi 附上你的代码

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jicvi

@alresvor 解决了 使用sum(1) 和count(1)都会报错

db := global.Db.Table(m.table)
sql := db.Select("SUM(order.amount) as title_amount")
sql = sql.Where("TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW())")
err := db.Scan(&counts).Count(&counts.OrderCount).Error

用.Count不会报错 Scan(&counts).Count(&counts.OrderCount)

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: alresvor

@jicvi 你这用法就是错的,虽然你之前没回复我就大概猜到了,你应该查询后使用 Row() 命令,其实就是转成了标准库的用法

// 这里举个例子,没有测试过
tx := sql.Where("TO_DAYS(from_unixtime(order.create_time)) = TO_DAYS(NOW())").Row() // 这里用 Row()
tx.Scan(&counts.OrderCount) // 然后这样,跟标准库是一样的

具体的你看一下文档吧,还是之前的链接

Comment From: jicvi

@alresvor 好的, 我试一下, 非常感谢