gorm中自己寫sql的方法實現

2022-05-19 14:45:12 字數 522 閱讀 2849

type result struct 

var result result

dao.db(dao.homeworktable).raw("select sum(round((unix_timestamp(finishtime)-unix_timestamp(stime))/60)) as total from homework where remarks !='' and ctime = to_days(now()) and finishtime = to_days(now())").select("total").scan(&result)

建立乙個結構體,其結構體中的變數是我們要返回的資料,

在db.raw方法中寫我們的sql語句和佔位符引數,

通過scan方法把sql返回的資料放入我們的結構體中,

結構體的變數名稱和sql返回的欄位名一致

SQL中Group分組獲取Top N 方法實現

有產品表,包含id,name,city,addtime四個字段,因報表需要按城市分組,統計每個城市的最新10個產品,便向該表中插入了100萬資料,做了如下系列測試 create table dbo products id int identity 1,1 not null name nvarchar...

自己寫的常用方法(Swfit版)

記載一些常用的公共方法 不斷更新中 判斷字串是否為空 parameter str string returns true or false class func judgeisemptywithstring str string bool return false 設定uibutton圓角 clas...

幾個自己寫的簡單的字元處理方法(更新中)

using system namespace public endregion region 字元替換為html字串 字元替換方法 回車 空格 待替換字串 替換後的字串 public static string replacecharhtml string str endregion region ...