select a.*,count(distinct b.id) as 'topicNum',count(distinct c.id) as 'groupNum' from resource_instance_rocketmq a left join resource_rocketmq_topic b on a.instance_id = b.instance_id left join resource_rocketmq_group c on a.instance_id = c.instance_id where a.instance_name like '%gaotu%' group by a.instance_id order by topicNum asc limit 3 offset 2
时间: 2024-02-04 14:03:46 浏览: 111
以下是将该 SQL 查询语句转换为 Golang 代码的示例:
```go
package main
import (
"context"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "user:password@tcp(host:port)/database")
if err != nil {
panic(err.Error())
}
defer db.Close()
query := `
SELECT a.*, COUNT(DISTINCT b.id) AS topicNum, COUNT(DISTINCT c.id) AS groupNum
FROM resource_instance_rocketmq a
LEFT JOIN resource_rocketmq_topic b ON a.instance_id = b.instance_id
LEFT JOIN resource_rocketmq_group c ON a.instance_id = c.instance_id
WHERE a.instance_name LIKE ?
GROUP BY a.instance_id
ORDER BY topicNum ASC
LIMIT 3 OFFSET 2
`
rows, err := db.QueryContext(context.Background(), query, "%gaotu%")
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
// 读取查询结果,并进行处理
}
}
```
请将代码中的 `user`、`password`、`host`、`port`、`database` 分别替换为您的 MySQL 数据库的实际连接信息。同时,您需要安装 `go-sql-driver/mysql` 包以便 Golang 代码连接 MySQL 数据库。请注意,为了防止 SQL 注入攻击,我们使用了 `?` 占位符,并将查询条件 `"gaotu"` 作为参数传入 `QueryContext` 方法中。
阅读全文