5
Golang gorm 怎么跨库查询 MySQL?
source link: https://www.v2ex.com/t/842781
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
17 条回复 • 2022-03-25 20:28:29 +08:00
Cheivin 1 天前
select * from `databaseName`.`tableName`
xiancai 1 天前
1. 使用 root 账号,拥有多个库的权限,按照 1 楼的方法进行查询
2. 建立多个连接,每个连接对应指定的库(为什么:为了安全,每个库都应该有单独的账号密码)
3. 不要跨库
2. 建立多个连接,每个连接对应指定的库(为什么:为了安全,每个库都应该有单独的账号密码)
3. 不要跨库
imherer 1 天前
db 应该不多吧? 直接按 db 初始化多个 db 对象呢?
Cheivin 1 天前 1
1. 连接数据库的时候,你可以不指定默认的 DbName
dsn := "user:pass@tcp(127.0.0.1:3306)/?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
2. 在声明模型的时候,通过 TableName() string 指定模型的数据库和表名
3. 或者查询的时候,通过 db.Table("databaseName.tableName") 指定指定库的表
dsn := "user:pass@tcp(127.0.0.1:3306)/?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
2. 在声明模型的时候,通过 TableName() string 指定模型的数据库和表名
3. 或者查询的时候,通过 db.Table("databaseName.tableName") 指定指定库的表
lasuar 23 小时 58 分钟前 1
db.table("T1 alias_t1").leftjoin("T2 alias_t2")... 或者 直接 db.raw/exec("原生 sql 该怎么写就怎么写")
superfatboy 21 小时 25 分钟前
多看文档,能初始化多个实例
jessun1990 14 小时 11 分钟前
啊,我这里也没有看到那个文档。
我的做法是在每次查询前面加一个 "use xxxx;"
我的做法是在每次查询前面加一个 "use xxxx;"
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK