gorm icon indicating copy to clipboard operation
gorm copied to clipboard

sql查询假如在SELECT 里面带有子查询的问题

Open longrbl opened this issue 2 years ago • 2 comments

下面是代码

db := public.OpenMysql()
	var datat = make(map[string]interface{})
	err := db.Raw("SELECT n.appstore,n.apkurl,n.versionnumber,n.serialnumber,n.isforce,n.hasupdate,n.isignorable,(SELECT content FROM zt_app_edition_updatelog WHERE appid=n.id AND `language`=1 ) AS updatelog FROM zt_app_edition AS n WHERE n.isdelete=0 ORDER BY n.id DESC LIMIT 1").Scan(&datat).Error
	log.Printf("%+v", datat)
	log.Println(err)

结果一样但是写法不一样的sql,可以得到一样的结果(可以得到想要的结果 updatelog 字段不为空)

db := public.OpenMysql()
	var datat = make(map[string]interface{})
	err := db.Raw("SELECT e.*,ep.id AS eid,ep.`content` as updatelog  FROM zt_app_edition AS e LEFT JOIN zt_app_edition_updatelog AS ep ON e.id=ep.appid WHERE ep.`language`=1 ORDER BY e.id DESC LIMIT 1").Scan(&datat).Error

	log.Printf("%+v", datat)
	log.Println(err)

下面是结果

2022/07/20 16:45:02 map[apkurl:/upload/pic/20220709/1657353567225774247.apk apps
tore: hasupdate:1 isforce:0 isignorable:0 serialnumber: updatelog:<nil> versionn
umber:1.21]
2022/07/20 16:45:02 <nil>

问题 上面执行的sql 在mysql工具执行是ok的可以正确获取到数据,通过gorm 就无法获取到 updatelog 字段的数据 无论datat是map 还是结构体, 这个问题是在哪?给我整不会了 SELECT 内不能进行子查询吗?

longrbl avatar Jul 20 '22 08:07 longrbl

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

github-actions[bot] avatar Jul 20 '22 09:07 github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

github-actions[bot] avatar Jul 27 '22 20:07 github-actions[bot]