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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 gorm 中處理多個查詢

如何在 gorm 中處理多個查詢

Go
qq_花開花謝_0 2022-12-13 16:32:02
我正在嘗試使用 gorm 處理嵌套查詢,但我很難弄清楚這一點。查詢:SELECT smId AS 'slimeId', slStatus, slPPIV, slHighRiskSituation, (SELECT count(rnIg) FROM ruins WHERE rnSquidStatus = 'Holding on'AND rnSmId = smId) AS 'holdingOn',(SELECT count(rnIg) FROM ruins WHERE rnSquidStatus = 'In system' AND rnSmId = smId) AS 'inSystem', smSuspectedCorona,IF(smStatusClosed != 0, 1, 0) as statusClosed, smSurge, (SELECT GROUP_CONCAT(rnName) FROM ruins WHERE rnSquidStatus = 'Holding on' AND rnSmId = smId) as 'ruinsOnHold',(SELECT GROUP_CONCAT(rnName) FROM ruins WHERE rnSquidStatus = 'In system' AND rnSmId = smId)as 'ruinsInSystem' FROM slimes WHERE slId != 0  GROUP BY slId HAVING slId > 0所以我可以完成一些非?;A(chǔ)的工作,問題是當(dāng)嵌套出現(xiàn)時我不知道在那種情況下該怎么做var data []SlimeResponsedb := service.gormdbdb = db.Select("smId as slimeId", "slStatus", "slPPIV", "slHighRiskSituation")db = db.Where("smId != ?", 0).Group("smId ").Having("smId > ?", 0)db = db.Table("slimes").Find(&data)嘗試使用Where方法但效果不一樣Joins但無法正常工作
查看完整描述

1 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗 獲得超4個贊

您可以使用subQuery

var data []SlimeResponse

db := service.gormdb

subQueryHoldingOn := db.

  Select("count(rnIg)").

  Where("rnSquidStatus = 'In system' AND rnSmId = ?", smId).

  Table("ruins")

subQueryInSystem := db.

  Select("count(rnIg)").

  Where("rnSquidStatus = 'Holding on' AND rnSmId = ?", smId).

  Table("ruins")

groupSubQueryHoldingOn := db.

  Select("GROUP_CONCAT(rnIg)").

  Where("rnSquidStatus = 'In system' AND rnSmId = ?", smId).

  Table("ruins")

groupSubQueryInSystem := db.

  Select("GROUP_CONCAT(rnIg)").

  Where("rnSquidStatus = 'Holding on' AND rnSmId = ?", smId).

  Table("ruins")

db = db.Select("smId as slimeId, slStatus, slPPIV, slHighRiskSituation, (?) as holdingOn, (?) as inSystem, smSuspectedCorona, IF(smStatusClosed != 0, 1, 0) as statusClosed, smSurge, (?) as ruinsOnHold, (?) as ruinsInSystem", subQueryHoldingOn, subQueryInSystem, groupSubQueryHoldingOn, groupSubQueryInSystem)

db = db.Where("smId != ?", 0).Group("smId ").Having("smId > ?", 0)

db = db.Table("slimes").Find(&data)

或者

可以直接傳入select部分Select

var data []SlimeResponse

db := service.gormdb

db = db.Select(

  `

    smId AS 'slimeId',

    slStatus,

    slPPIV,

    slHighRiskSituation, 

    (

      SELECT count(rnIg) FROM ruins WHERE rnSquidStatus = 'Holding on' AND rnSmId = ?

    ) AS 'holdingOn',

    (

      SELECT count(rnIg) FROM ruins WHERE rnSquidStatus = 'In system' AND rnSmId = ?

    ) AS 'inSystem',

    smSuspectedCorona,

    IF(smStatusClosed != 0, 1, 0) as statusClosed,

    smSurge, 

    (

      SELECT GROUP_CONCAT(rnName) FROM ruins WHERE rnSquidStatus = 'Holding on' AND rnSmId = ?

    ) as 'ruinsOnHold',

    (

      SELECT GROUP_CONCAT(rnName) FROM ruins WHERE rnSquidStatus = 'In system' AND rnSmId = ?

    ) as 'ruinsInSystem'

  `, smId, smId, smId, smId

)

db = db.Where("smId != ?", 0).Group("smId ").Having("smId > ?", 0)

db = db.Table("slimes").Find(&data)


查看完整回答
反對 回復(fù) 2022-12-13
  • 1 回答
  • 0 關(guān)注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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