*Join Series Methods

  1. LeftJoin Left associative query.
  2. RightJoin Right associative query.
  3. InnerJoin Inner associative query.

Model Query - Join - 图1note

Actually, we do not recommend using Join for join queries, especially in scenarios with large data volumes and high concurrent request volumes, as it can easily cause performance issues and increase maintenance complexity. It is recommended to use it when necessary. Additionally, you can refer to Model Association - ScanList section where the database is responsible only for storing data and performing simple single-table operations, with data aggregation being implemented at the code level using the functionalities provided by ORM.

Example usage:

  1. // Query the first record that meets the condition
  2. // SELECT u.*,ud.site FROM user u LEFT JOIN user_detail ud ON u.uid=ud.uid WHERE u.uid=1 LIMIT 1
  3. g.Model("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.site").Where("u.uid", 1).One()
  4. // Query specific field values
  5. // SELECT ud.site FROM user u RIGHT JOIN user_detail ud ON u.uid=ud.uid WHERE u.uid=1 LIMIT 1
  6. g.Model("user u").RightJoin("user_detail ud", "u.uid=ud.uid").Fields("ud.site").Where("u.uid", 1).Value()
  7. // Grouping and ordering
  8. // SELECT u.*,ud.city FROM user u INNER JOIN user_detail ud ON u.uid=ud.uid GROUP BY city ORDER BY register_time asc
  9. g.Model("user u").InnerJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.city").Group("city").Order("register_time asc").All()
  10. // Join query without using join
  11. // SELECT u.*,ud.city FROM user u,user_detail ud WHERE u.uid=ud.uid
  12. g.Model("user u,user_detail ud").Where("u.uid=ud.uid").Fields("u.*,ud.city").All()

Custom Table Alias

  1. // SELECT * FROM `user` AS u LEFT JOIN `user_detail` as ud ON(ud.id=u.id) WHERE u.id=1 LIMIT 1
  2. g.Model("user", "u").LeftJoin("user_detail", "ud", "ud.id=u.id").Where("u.id", 1).One()
  3. g.Model("user").As("u").LeftJoin("user_detail", "ud", "ud.id=u.id").Where("u.id", 1).One()

*JoinOnFields Series Methods

The methods LeftJoinOnFields/RightJoinOnFields/InnerJoinOnFields allow specifying fields and operators for join queries. Example usage:

  1. // Query the first record that meets the condition
  2. // SELECT user.*,user_detail.address FROM user LEFT JOIN user_detail ON (user.id = user_detail.uid) WHERE user.id=1 LIMIT 1
  3. g.Model("user").LeftJoinOnFields("user_detail", "id", "=", "uid").Fields("user.*,user_detail.address").Where("id", 1).One()
  4. // Query multiple records
  5. // SELECT user.*,user_detail.address FROM user RIGHT JOIN user_detail ON (user.id = user_detail.uid)
  6. g.Model("user").RightJoinOnFields("user_detail", "id", "=", "uid").Fields("user.*,user_detail.address").All()

Example Usage with dao

  1. // SELECT resource_task_schedule.id,...,time_window.time_window
  2. // FROM `resource_task_schedule`
  3. // LEFT JOIN `time_window` ON (`resource_task_schedule`.`resource_id`=`time_window`.`resource_id`)
  4. // WHERE (time_window.`status`="valid") AND (`time_window`.`start_time` <= 3600)
  5. var (
  6. orm = dao.ResourceTaskSchedule.Ctx(ctx)
  7. tsTable = dao.ResourceTaskSchedule.Table()
  8. tsCls = dao.ResourceTaskSchedule.Columns()
  9. twTable = dao.TimeWindow.Table()
  10. twCls = dao.TimeWindow.Columns()
  11. scheduleItems []scheduleItem
  12. )
  13. orm = orm.FieldsPrefix(tsTable, tsCls)
  14. orm = orm.FieldsPrefix(twTable, twCls.TimeWindow)
  15. orm = orm.LeftJoinOnField(twTable, twCls.ResourceId)
  16. orm = orm.WherePrefix(twTable, twCls.Status, "valid")
  17. orm = orm.WherePrefixLTE(twTable, twCls.StartTime, 3600)
  18. err = orm.Scan(&scheduleItems)
  1. // SELECT DISTINCT resource_info.* FROM `resource_info`
  2. // LEFT JOIN `resource_network` ON (`resource_info`.`resource_id`=`resource_network`.`resource_id`)
  3. // WHERE (`resource_info`.`resource_id` like '%10.0.1.3%')
  4. // or (`resource_info`.`resource_name` like '%10.0.1.3%')
  5. // or (`resource_network`.`vip`like '%10.0.1.3%')
  6. // ORDER BY `id` Desc LIMIT 0,2
  7. var (
  8. orm = dao.ResourceInfo.Ctx(ctx).OmitEmpty()
  9. rTable = dao.ResourceInfo.Table()
  10. rCls = dao.ResourceInfo.Columns()
  11. nTable = dao.ResourceNetwork.Table()
  12. nCls = dao.ResourceNetwork.Columns()
  13. )
  14. orm = orm.LeftJoinOnField(nTable, rCls.ResourceId)
  15. orm = orm.WherePrefix(rTable, do.ResourceInfo{
  16. AppId: req.AppIds,
  17. ResourceId: req.ResourceIds,
  18. Region: req.Regions,
  19. Zone: req.Zones,
  20. ResourceName: req.ResourceNames,
  21. Status: req.Statuses,
  22. BusinessType: req.Products,
  23. Engine: req.Engines,
  24. Version: req.Versions,
  25. })
  26. orm = orm.WherePrefix(nTable, do.ResourceNetwork{
  27. Vip: req.Vips,
  28. VpcId: req.VpcIds,
  29. SubnetId: req.SubnetIds,
  30. })
  31. // Fuzzy like querying.
  32. if req.Key != "" {
  33. var (
  34. keyLike = "%" + req.Key + "%"
  35. )
  36. whereFormat := fmt.Sprintf(
  37. "(`%s`.`%s` like ?) or (`%s`.`%s` like ?) or (`%s`.`%s`like ?) ",
  38. rTable, rCls.ResourceId,
  39. rTable, rCls.ResourceName,
  40. nTable, nCls.Vip,
  41. )
  42. orm = orm.Where(whereFormat, keyLike, keyLike, keyLike)
  43. }
  44. // Resource items.
  45. err = orm.Distinct().FieldsPrefix(rTable, "*").Order(req.Order, req.OrderDirection).Limit(req.Offset, req.Limit).Scan(&res.Items)