用gorm完成根据店铺id查用户类型user_type为1的用户,可把手机号(mobile)、昵称(nickname)作为查询条件
时间: 2023-05-18 18:05:00 浏览: 65
可以使用以下代码完成该查询:
```go
db.Where("store_id = ? AND user_type = ?", storeID, 1).
Select("mobile, nickname").
Find(&users)
```
其中,`storeID`为店铺ID,`users`为查询结果的存储变量。`db`为已经初始化好的`gorm.DB`对象。
相关问题
用goland,gorm,mysql完成查询客服功能,根据店铺id查用户类型user_type为1的用户,可把手机号(mobile)、昵称(nickname)作为查询条件
可以使用以下代码完成查询客服功能:
```go
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type User struct {
ID uint
Mobile string
Nickname string
UserType int
}
func main() {
db, err := gorm.Open("mysql", "user:password@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
var users []User
db.Where("user_type = ? AND shop_id = ?", 1, shopID).Find(&users)
for _, user := range users {
fmt.Printf("Mobile: %s, Nickname: %s\n", user.Mobile, user.Nickname)
}
}
```
其中,`shopID`是你要查询的店铺ID。
使用jinzhu的gorm根据父id查询出所有的子id
如果你使用的是 jinzhu 的 GORM v1,你可以使用 `Where` 方法和 `Find` 方法来查询所有的子元素。以下是示例代码:
```go
type Category struct {
ID uint
ParentID uint
}
func FindChildren(db *gorm.DB, parentID uint) ([]Category, error) {
var categories []Category
err := db.Where("parent_id = ?", parentID).Find(&categories).Error
if err != nil {
return nil, err
}
for i := range categories {
subCategories, err := FindChildren(db, categories[i].ID)
if err != nil {
return nil, err
}
categories = append(categories, subCategories...)
}
return categories, nil
}
```
该函数使用递归方式查询所有的子元素,并将它们添加到一个切片中。请注意,这种方法可能会导致性能问题,因为它需要多次查询数据库。如果你的数据集很大,你可能需要使用其他的查询方法来提高性能。
如果你使用的是 GORM v2,你可以使用 `Recursive` 方法来查询所有的子元素。以下是示例代码:
```go
type Category struct {
ID uint
ParentID uint
}
func FindChildren(db *gorm.DB, parentID uint) ([]Category, error) {
var categories []Category
err := db.Where("parent_id = ?", parentID).Preload("Children", func(db *gorm.DB) *gorm.DB {
return db.Recursive(10)
}).Find(&categories).Error
if err != nil {
return nil, err
}
return categories, nil
}
```
该函数使用 `Preload` 方法和 `Recursive` 方法来查询所有的子元素,并将它们预加载到 `Children` 字段中。请注意,`Recursive` 方法需要指定递归的深度,以避免无限递归。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)