第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 GORM 執(zhí)行聯(lián)接并遍歷結(jié)果行?

如何使用 GORM 執(zhí)行聯(lián)接并遍歷結(jié)果行?

users使用gorm,我為和product_prices表創(chuàng)建了以下模型// model for table `users`type User struct {    Id            uint64 `json:"id" gorm:"primaryKey"`    Email         string `json:"email" gorm:"unique"`    Password      []byte `json:"-"`    CreatedOn     time.Time `json:"created_on" gorm:"<-:create"`    UpdatedOn     time.Time `json:"updated_on"`}// model for table `product_prices`type ProductPrice struct {    Id           uint64 `json:"id" gorm:"primaryKey"`    Price        float64 `json:"price"`    Direction    string  `json:"direction"`    NotificationMethod string  `json:"notification_method"`    CreatedOn    time.Time  `json:"created_on,omitempty" gorm:"<-:create"`    UpdatedOn    time.Time  `json:"updated_on,omitempty"`    LastNotified time.Time  `json:"last_notified,omitempty"`    UserId       uint64  `json:"user_id"`    User         User    `json:"-" gorm:"foreignKey:UserId"`}我想做一些類(lèi)似在product_prices和users表之間執(zhí)行以下sql連接的事情select product_prices.id, product_prices.price, product_prices.created_on, users.email as user_email, users.created_on as user_created_on from product_prices join users on product_prices.user_id=users.id;這是我嘗試過(guò)但仍然不走運(yùn)的許多事情之一,這要?dú)w功于文檔頁(yè)面https://gorm.io/docs/query.html#Joins不夠清晰, 我還想遍歷行按照這里的文檔https://gorm.io/docs/query.html#Joins我嘗試了以下// struct for result of join querytype ProductPriceUser struct {    Id                     uint64 `json:"id"`    Price                  float64 `json:"price"`    CreatedOn              time.Time  `json:"created_on,omitempty"`    UserEmail              User `json:"user_email"`    UserCreatedOn          User `json:"user_created_on"`}但是我遇到了各種各樣的錯(cuò)誤,我做錯(cuò)了什么,我如何得到我想要的,如上所述?謝謝!
查看完整描述

1 回答

?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

一種可能的解決方案是做這樣的事情(假設(shè)你的數(shù)據(jù)庫(kù)表被命名為product_pricesand users):


list := []ProductPriceUser{}


err := database.DB.Table("product_prices").

            Join("JOIN users ON users.id=product_prices.user_id").

            Select("product_prices.id, product_prices.price, product_prices.created_on, users.email as user_email, users.created_on as user_created_on").

            Find(&list).Error

if err != nil {

  // handle error

}

然后,您可以遍歷list以獲取每條記錄。


查看完整回答
反對(duì) 回復(fù) 2022-10-31
  • 1 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)