mysql两个表联合查询的时候,如果两个表有相同的字段名称会做成不匹配或者匹配错误。 #2192

Open
opened 2022-11-08 08:56:01 +00:00 by daozhao · 2 comments

请问这个问题可以解决么?除了写sql的时候,指定重复字段用别名。还有其他更好的方法么?

type TeachingResource struct {
ID int64 xorm:"'id' bigint pk autoincr notnull" // 主键
Name string xorm:"'name' varchar(256) notnull" // 名称
}

type ClassResource struct {
ID int64 xorm:"'id' bigint pk autoincr notnull" // 主键
Name string xorm:"'name' varchar(256) notnull" // 名称
ResourceID int64 xorm:"'resource_id' bigint index" // 资源ID

}

type CombinedAdminCensoringProgress struct {
Resource *TeachingResource xorm:"extends"
Myclass *ClassResource xorm:"extends"
}

var rs = make([]*CombinedAdminCensoringProgress, 0)
err2 := engine.
SQL("SELECT teaching_resource.id,teaching_resource.name,teaching_resource.class_id,class_resource.id,class_resource.class_id FROM teaching_resource ,class_resource where teaching_resource.id = class_resource.resource_id").
Find(&rs)

这样可以正确查询到希望的内容。但是如果CombinedAdminCensoringProgress的结构写发如下。
type CombinedAdminCensoringProgress struct {
Myclass *ClassResource xorm:"extends"
Resource *TeachingResource xorm:"extends"
}

这样查询结果的内容就不正确了。myclass和resource的id 和 name字段内容会对调了。这样是不正确的。有没有办法可以正确映射。
我跟踪过代码进入xorm的内部。发觉mysql返回的字段名称不带表前序。所以感觉这个bug无解。

我目前程序的情况比这个复杂很多。我最后debug的过程发现这个问题。我是因为新版本程序增加了字段,其中新增的一个字段和其他联合查询的表有相同的名字,但是字段类型不一样,导致旧版程序不修改代码的情况下出现映射失败。

请问这个问题可以解决么?除了写sql的时候,指定重复字段用别名。还有其他更好的方法么? type TeachingResource struct { ID int64 `xorm:"'id' bigint pk autoincr notnull"` // 主键 Name string `xorm:"'name' varchar(256) notnull"` // 名称 } type ClassResource struct { ID int64 `xorm:"'id' bigint pk autoincr notnull"` // 主键 Name string `xorm:"'name' varchar(256) notnull"` // 名称 ResourceID int64 `xorm:"'resource_id' bigint index"` // 资源ID } type CombinedAdminCensoringProgress struct { Resource *TeachingResource `xorm:"extends"` Myclass *ClassResource `xorm:"extends"` } var rs = make([]*CombinedAdminCensoringProgress, 0) err2 := engine. SQL("SELECT teaching_resource.id,teaching_resource.name,teaching_resource.class_id,class_resource.id,class_resource.class_id FROM `teaching_resource` ,`class_resource` where teaching_resource.id = class_resource.resource_id"). Find(&rs) 这样可以正确查询到希望的内容。但是如果CombinedAdminCensoringProgress的结构写发如下。 type CombinedAdminCensoringProgress struct { Myclass *ClassResource `xorm:"extends"` Resource *TeachingResource `xorm:"extends"` } 这样查询结果的内容就不正确了。myclass和resource的id 和 name字段内容会对调了。这样是不正确的。有没有办法可以正确映射。 我跟踪过代码进入xorm的内部。发觉mysql返回的字段名称不带表前序。所以感觉这个bug无解。 我目前程序的情况比这个复杂很多。我最后debug的过程发现这个问题。我是因为新版本程序增加了字段,其中新增的一个字段和其他联合查询的表有相同的名字,但是字段类型不一样,导致旧版程序不修改代码的情况下出现映射失败。
Owner

extends顺序和join顺序保持一致就可以了。

extends顺序和join顺序保持一致就可以了。
Author

你这个顺序一致就可以。我明白,我也知道。
我现在的问题是。后端程序升级的时候,希望新旧两个版本的程序同时运行,对应新旧两个版本的前端程序。
假设A B两个表联合查询,新版本程序 如果A表增加了一个字段和B表的字段同名。
那么旧版本的程序就会出问题。这样变了旧版本程序也要做适应性更新。
现实的情况表比较多,联合查询也多。然后这样每次数据库增加字段。都要新旧后台程序都要把所有功能都测试一遍,这样工作量大了很多。

你这个顺序一致就可以。我明白,我也知道。 我现在的问题是。后端程序升级的时候,希望新旧两个版本的程序同时运行,对应新旧两个版本的前端程序。 假设A B两个表联合查询,新版本程序 如果A表增加了一个字段和B表的字段同名。 那么旧版本的程序就会出问题。这样变了旧版本程序也要做适应性更新。 现实的情况表比较多,联合查询也多。然后这样每次数据库增加字段。都要新旧后台程序都要把所有功能都测试一遍,这样工作量大了很多。
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2192
No description provided.