php小編西瓜在這篇文章中將為大家介紹Golang sqlx如何將多對多連接的右側轉換為陣列。 Golang是一種強大的程式語言,而sqlx是一個流行的Golang資料庫工具。在使用sqlx進行多對多連接時,我們經常需要將右側的結果轉換為數組,以便更方便地進行處理和操作。本文將詳細介紹如何使用sqlx和一些實例來示範這個過程,希望能對大家有幫助。
問題內容
我有兩張桌子 具有 unit 外鍵的 unit 和 unitimage。
我執行這個sql查詢
select un.*, array_agg(row(ui.is_main, ui.image, ui.id)) as unit_images from unit un inner join unitimage ui on ui.unit_id = un.id group by un.id;
作為回應,我得到了所有單位字段並像這樣加入了字段unit_images
{"(f,photos/units/unit_37/90_big.jpg,108)","(f,photos/units/unit_37/91_big.jpg,109)","(f,photos/units/unit_37/92_big.jpg,110)","(f,photos/units/unit_37/93_big.jpg,111)"}
go 語言結構體
type unit struct { id *int `json:"id" db:"id"` name *string `json:"name" db:"name"` ... a lot of fields unitimages []unitimages `json:"unit_images" db:"unit_images"` } type unitimages struct { id *int `json:"id" db:"id"` image *string `json:"image" db:"image"` ismain *bool `json:"is_main" db:"is_main"` }
sqlx程式碼
query := fmt.Sprintf("SELECT un.*, array_agg(ROW(ui.id, ui.image, ui.is_main)) as unit_images FROM %s un INNER JOIN %s ui ON ui.unit_id = un.id GROUP BY un.id", unitsTable, unitImagesTable) err := r.db.Select(&units, query)
我收到錯誤"sql: 列索引45 上的掃描錯誤,名稱\"unit_images\": 不支援掃描,將driver.value 類型[]uint8 儲存到類型*[]*unitimages"
我是 golang 的新手,我想獲得有關如何解決此問題的任何提示。也許我選擇了錯誤的方式。
我想知道解決此問題的正確方法。
解決方法
看起來回傳的 sql 結果是:
un.id | un.name | un.description | unit_images ------+---------+----------------+--------------------------------------------------------------------- 1 | unit a | description a | [(true, 'image1.jpg', 1), (false, 'image2.jpg', 2), (false, 'image3.jpg', 3)] 2 | unit b | description b | [(true, 'image4.jpg', 4), (true, 'image5.jpg', 5), (false, 'image6.jpg', 6)] 3 | unit c | description c | [(true, 'image7.jpg', 7), (false, 'image8.jpg', 8), (false, 'image9.jpg', 9)]
所以
`unitimages []unitimages `json:"unit_images" db:"unit_images"`
這是正確的想法,您有一個 unitimages 陣列。但是:
id *int `json:"id" db:"id"` image *string `json:"image" db:"image"` ismain *bool `json:"is_main" db:"is_main"`
請注意,id
、image
、is_main
沒有 sql 列,因此 go 無法對應它們。
輕鬆修復將 unitimages 更改為 []any,然後自己將內容投射到該數組中,如下所示:
for _, item := range thing.UnitImages { isMain := item[0].(bool) image := item[1].(string) id := item[2].(int64) //TODO do something with these vars } Or you could use `pg.StringArray` type.
以上是Golang sqlx 將多對多連線的右側轉換為陣列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

golangisidealforperformance-Critical-clitageAppations and ConcurrentPrompromming,而毛皮刺激性,快速播種和可及性。 1)forhigh-porformanceneeds,pelectgolangduetoitsefefsefefseffifeficefsefeflicefsiveficefsiveandconcurrencyfeatures.2)fordataa-fordataa-fordata-fordata-driventriventriventriventriventrivendissp pynonnononesp

Golang通過goroutine和channel實現高效並發:1.goroutine是輕量級線程,使用go關鍵字啟動;2.channel用於goroutine間安全通信,避免競態條件;3.使用示例展示了基本和高級用法;4.常見錯誤包括死鎖和數據競爭,可用gorun-race檢測;5.性能優化建議減少channel使用,合理設置goroutine數量,使用sync.Pool管理內存。

Golang更適合系統編程和高並發應用,Python更適合數據科學和快速開發。 1)Golang由Google開發,靜態類型,強調簡潔性和高效性,適合高並發場景。 2)Python由GuidovanRossum創造,動態類型,語法簡潔,應用廣泛,適合初學者和數據處理。

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Go語言在並發編程、性能、學習曲線等方面有獨特優勢:1.並發編程通過goroutine和channel實現,輕量高效。 2.編譯速度快,運行性能接近C語言。 3.語法簡潔,學習曲線平緩,生態系統豐富。

Golang和Python的主要區別在於並發模型、類型系統、性能和執行速度。 1.Golang使用CSP模型,適用於高並發任務;Python依賴多線程和GIL,適合I/O密集型任務。 2.Golang是靜態類型,Python是動態類型。 3.Golang編譯型語言執行速度快,Python解釋型語言開發速度快。

Golang通常比C 慢,但Golang在並發編程和開發效率上更具優勢:1)Golang的垃圾回收和並發模型使其在高並發場景下表現出色;2)C 通過手動內存管理和硬件優化獲得更高性能,但開發複雜度較高。

Golang在雲計算和DevOps中的應用廣泛,其優勢在於簡單性、高效性和並發編程能力。 1)在雲計算中,Golang通過goroutine和channel機制高效處理並發請求。 2)在DevOps中,Golang的快速編譯和跨平台特性使其成為自動化工具的首選。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境