亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

左連接如何與 sqlx 一起工作

左連接如何與 sqlx 一起工作

Go
鴻蒙傳說 2023-04-17 16:04:22
我正在嘗試內部連接兩個表person并profile使用一個簡單的查詢,該查詢似乎適用于 mysql 但不適用于 sqlx。這是我的代碼:package main import (    "fmt"    "github.com/jmoiron/sqlx"    _ "github.com/go-sql-driver/mysql")type Person struct {    Id      int64   `db:"id"`    Name    string  `db:"name"`    Email   string  `db:"email"`}type Profile struct {    Id          int64   `db:"id"`    Face        string  `db:"face"`    Hair        string  `db:"hair"`    Person}func main() {    DB, err := sqlx.Connect("mysql", "root:hackinitiator@/dusk")    if err == nil {        fmt.Println("sucess!!")    }     var q []Profile    DB.Select(&q, "select person.id, person.name, person.email, profile.id, profile.face, profile.hair from profile left join person on person.id = profile.person_id")    fmt.Println(q)}mysql 查詢產生以下輸出:+------+------+---------+----+----------+--------+| id   | name | email   | id | face     | hair   |+------+------+---------+----+----------+--------+|    1 | yoda | nomail  |  1 | round    | brown  ||    5 | han  | nomail1 |  3 | circle   | red    ||    6 | yun  | nomail2 |  4 | triangle | yellow ||    7 | chi  | nomail3 |  5 | square   | green  |+------+------+---------+----+----------+--------+這很好,但我的 go 程序沒有按預期響應。該結構無法捕獲配置文件 ID(輸出中為空),并且人員 ID 已替換為配置文件 ID。下面是輸出(格式化):[{0 round brown {1 yoda nomail}} {0 circle red {3 han nomail1}} {0 triangle yellow {4 yun nomail2}} {0 square green {5 chi nomail3}}]我無法弄清楚出了什么問題。
查看完整描述

3 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

我做了一些更改,使我可以無錯誤地運行程序,并且無需重命名 db 標簽。首先,我在配置文件結構中添加了以下代碼,讓查詢識別人員結構

Person?`db:"person"`

在此之后,我將 SQL 查詢字符串更改為以下代碼

DB.Select(&q,?`select?person.id?"person.id",?person.name?"person.name",?person.email?"person.email",?profile.*?from?profile?left?join?person?on?person.id?=?profile.person_id`)


查看完整回答
反對 回復 2023-04-17
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

該錯誤是由于id從結果中返回兩列但將結果存儲在兩個結構中具有相同字段名稱 id 的結構中,您將其實例傳遞給 DB.Select。嘗試捕獲單個 id 列并將其傳遞給結構。


傳遞多個列但不同的列名,您可以將其用作別名。列別名將是您在其中掃描數據的 Person 結構中的字段:


type Person struct {

    PersonId    int64   `db:"personId"`

    Name        string  `db:"name"`

    Email       string  `db:"email"`

}


var q []Profile

DB.Select(&q, "select person.id as personId, person.name, person.email, profile.id, profile.face, profile.hair from profile left join person on person.id = profile.person_id")

fmt.Println(q)


查看完整回答
反對 回復 2023-04-17
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

您需要像下面我描述的那樣更改結構db中的名稱person,因為會有兩列具有相同的名稱,id即它只掃描profile表中的最后一個 ID 而不是掃描person表,因此請按照下面提到的結構進行操作。


type Person struct {

    Id      int64   `db:"pId"`

    Name    string  `db:"name"`

    Email   string  `db:"email"`

}

然后用asfor person.idlike寫你的查詢


DB.Select(&q, "select (person.id) as pId, person.name, person.email, profile.id, profile.face, profile.hair from profile left join person on person.id = profile.person_id")



查看完整回答
反對 回復 2023-04-17
  • 3 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號