type Mycount struct { Usercount uint }
// 关于Gorm执行原生SQL // **********语句字段要小写************ // ***********查询用db.Raw,其他用db.Exec // *********** 字段大小写要对应上 ************** // *************** 注意要 defer rows.Close() var Mycount Mycount rows,_:= db.Debug().Raw("select Count(1) as Usercount from Users").Rows() defer rows.Close() for rows.Next() { rows.Scan(&Mycount.Usercount) } var users []User // 查询 执行用Scan 和Find 一样 db=db.Raw("select uid,user_name,age from Users").Scan(&users) //db=db.Raw("select uid,user_name,age from Users").Find(&users) fmt.Println("Users",users) // 更新和删除.插入用 Exec db= db.Exec("update Users set user_name=‘def‘ where uid=?",1) fmt.Println("更新了",db.RowsAffected,"条数据") db= db.Exec("delete from Users where uid=?",2) fmt.Println("更新了",db.RowsAffected,"条数据") return int32(Mycount.Usercount)