欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xorm -Alias,Asc,Desc方法实例

程序员文章站 2022-06-24 11:02:25
Alias(string)给Table设定一个别名 go package main import ( "fmt" _ "github.com/go sql driver/mysql" "github.com/go xorm/xorm" "log" "time" ) var engine xorm.E ......
  • 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
    */