xorm -Alias,Asc,Desc方法实例
阅读原文时间:2023年07月15日阅读:1
  • Alias(string)给Table设定一个别名

    package main
    
    import (
        "fmt"
        _ "github.com/go-sql-driver/mysql"
        "github.com/go-xorm/xorm"
        "log"
        "time"
    )
    
    var engine *xorm.Engine
    
    type User struct {
        Name string `xorm:"varchar(25) 'name'"`
        Id   int    `xorm:"pk 'id' autoincr"`
        CreatedAt time.Time `xorm:"created"`
    }
    
    func main() {
        var err error
        engine, err = xorm.NewEngine("mysql", "root:123456@/test")
        if err != nil {
            log.Fatal(err)
            return
        }
    err = engine.CreateTables(User{})
    if err != nil {
        log.Fatal(err)
        return
    }
    //插入数据部分 u := make([]User,3) u[0].Name="u0" u[1].Name="u1"
    _, err = engine.Insert(u)
    if err!=nil {
        log.Fatal(err)
        return
    }
    
    uu :=new(User)
    
    //查询结果为空
    has,err := engine.Alias("alias") .Where("alias.name = ?","u").Get(uu)
    if err!=nil{
        log.Fatal(err)
        return
    }
    fmt.Println("要查询的name是u的数据是否在数据库里:",has)
    if has {
        fmt.Println("uid:",uu.Id," uname:" ,uu.Name," utime:",uu.CreatedAt)
    }
    
    // 一个查询条件
    has,err = engine.Alias("alias") .Where("alias.name = ?","u0").Get(uu)
    if err!=nil{
        log.Fatal(err)
        return
    }
    fmt.Println("name是u0的数据是否在数据库里",has)
    if has {
        fmt.Println("    name是u0的数据在数据库里,而且他的uid:",uu.Id," uname:" ,uu.Name," utime:",uu.CreatedAt)
    }
    
    //and条件查询
    has,err = engine.Alias("alias") .Where("alias.name = ?","u0").And("alias.id =?","1").Get(uu)
    if err!=nil{
        log.Fatal(err)
        return
    }
    fmt.Println("name为u0,id为1的数据是否在数据库里",has)
    if has {
        fmt.Println("     name为u0,id为1的数据在数据库里,而且他的uid:",uu.Id," uname:" ,uu.Name," utime:",uu.CreatedAt)
    }
    
    //or条件查询
    has,err = engine.Alias("alias") .Where("alias.name = ?","u0").Or("alias.name =?","u").Get(uu)
    if err!=nil{
        log.Fatal(err)
        return
    }
    fmt.Println("name为u0或者为u的数据在数据库里",has)
    if has {
        fmt.Println("     name为u0或者为u的数据在数据库里,而且他的uid:",uu.Id," uname:" ,uu.Name," utime:",uu.CreatedAt)
    }
    /*
        输出:
            要查询的name是u的数据是否在数据库里: false
            name是u0的数据是否在数据库里 true
                name是u0的数据在数据库里,而且他的uid: 6  uname: u0  utime: 2019-05-26 14:17:51 +0800 CST
            name为u0,id为1的数据是否在数据库里 false
            name为u0或者为u的数据在数据库里 true
                 name为u0或者为u的数据在数据库里,而且他的uid: 6  uname: u0  utime: 2019-05-26 14:17:51 +0800 CST
    
    */
    }
  • Asc(…string)指定字段名正序排序/Desc(…string)指定字段名逆序排序

    package main
    
    import (
        "fmt"
        _ "github.com/go-sql-driver/mysql"
        "github.com/go-xorm/xorm"
        "log"
        "time"
    )
    
    var engine *xorm.Engine
    
    type User struct {
        Name      string    `xorm:"varchar(25) 'name'"`
        Id        int       `xorm:"pk 'id' autoincr"`
        CreatedAt time.Time `xorm:"created"`
    }
    
    func main() {
        var err error
        engine, err = xorm.NewEngine("mysql", "root:123456@/test")
        if err != nil {
            log.Fatal(err)
            return
        }
    err = engine.CreateTables(User{})
    if err != nil {
        log.Fatal(err)
        return
    }
    /* u := make([]User, 5) u[0].Name = "abcd" u[1].Name = "acbd" u[2].Name = "dbac" u[3].Name = "cbda" u[4].Name = "bdca"
    _, err = engine.Insert(u)
    if err != nil {
        log.Fatal(err)
        return
    }
    */ re := make([]User, 0)
    fmt.Println("升序之前:")
    err = engine.Find(&re)
    if err != nil {
        log.Fatal(err)
        return
    }
    for i, r := range re {
        fmt.Println("    index:", i, " rName:", r.Name)
    }
    
    re1 := make([]User, 0)
    fmt.Println("====================升序之后:")
    err = engine.Asc("name").Find(&re1)
    if err != nil {
        log.Fatal(err)
        return
    }
    
    for i, r := range re1 {
        fmt.Println("    index:", i, " rName:", r.Name)
    }
    
    fmt.Println("======================降序之后:")
    re2 := make([]User, 0)
    err = engine.Desc("name").Find(&re2)
    if err != nil {
        log.Fatal(err)
        return
    }
    
    for i, r := range re2 {
        fmt.Println("    index:", i, " rName:", r.Name)
    }
    
    fmt.Println("======================ORDERBY:")
    re3 := make([]User, 0)
    err = engine.OrderBy("name").Find(&re3)
    if err != nil {
        log.Fatal(err)
        return
    }
    
    for i, r := range re3 {
        fmt.Println("    index:", i, " rName:", r.Name)
    }
    
    fmt.Println("======================DESC:")
    re4 := make([]User, 0)
    err = engine.Desc("name").Find(&re4)
    if err != nil {
        log.Fatal(err)
        return
    }
    
    for i, r := range re4 {
        fmt.Println("    index:", i, " rName:", r.Name)
    }
    } /* 输出: 升序之前: index: 0 rName: abcd index: 1 rName: acbd index: 2 rName: dbac index: 3 rName: cbda index: 4 rName: bdca ====================升序之后: index: 0 rName: abcd index: 1 rName: acbd index: 2 rName: bdca index: 3 rName: cbda index: 4 rName: dbac ======================降序之后: index: 0 rName: dbac index: 1 rName: cbda index: 2 rName: bdca index: 3 rName: acbd index: 4 rName: abcd ======================ORDERBY: index: 0 rName: abcd index: 1 rName: acbd index: 2 rName: bdca index: 3 rName: cbda index: 4 rName: dbac ======================DESC: index: 0 rName: dbac index: 1 rName: cbda index: 2 rName: bdca index: 3 rName: acbd index: 4 rName: abcd */

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章