我有以下三个表:
create table A (
a_id varchar(256) not null unique,
a_name varchar(256)
);
create table B (
b_id varchar(256) not null,
b_a_id varchar(256) not null,
b_name varchar(256),
FOREIGN KEY (b_a_id) REFERENCES a (a_id)
);
create table C (
c_id varchar(256) not null,
c_a_id varchar(256) not null,
c_name varchar(256),
FOREIGN KEY (c_a_id) REFERENCES a (a_id)
);
insert into A(a_id, a_name) values('1234', 'a_name_1');
insert into B(b_id, b_a_id, b_name) values('B1','1234', 'b_name_1');
insert into B(b_id, b_a_id, b_name) values('B2','1234', 'b_name_2');
insert into C(c_id, c_a_id, c_name) values('C1','1234', 'c_name_1');
insert into C(c_id, c_a_id, c_name) values('C2','1234', 'c_name_2');
insert into C(c_id, c_a_id, c_name) values('C3','1234', 'c_name_3');
我在golang中有以下structs:
type A struct {
a_id string `db:"a_id"`
a_name string `db:"a_name"`
b *B `db:"b"`
c *C `db:"c"`
}
type B struct {
b_id string `db:"b_id"`
b_name string `db:"b_name"`
b_a_id string `db:"b_a_id"`
}
type C struct {
c_id string `db:"c_id"`
c_name string `db:"c_name"`
c_a_id string `db:"c_a_id"`
}
我想要扫描执行联接查询后得到的行:
SELECT * from A INNER JOIN B ON a_id=b_a_id inner join C on c_a_id=a_id;
使用Golang中的rows.StructScan()将其转换为Struct A,但我无法做到。我该如何将联接查询结果扫描到嵌套结构中,而且我不想逐个扫描每一列,因为联接查询的结果会有很多列。