MCPcopy Index your code
hub / github.com/TruthHun/DocHub / LeftJoinSqlBuild

Function LeftJoinSqlBuild

models/Models.go:399–441  ·  view source on GitHub ↗

左联合查询创建SQL语句 @param tables 需要作为联合查询的数据表。注意:数据表的第一个表是主表 @param on 联合查询的on查询条件,必须必表(tables)少一个。比如user表和user_info表做联合查询,那么on查询条件只有一个,必tables的数组元素少一个 @param fields 需要查询的字段 @param p

(tables []string, on []map[string]string, fields map[string][]string, p, listRows int, orderBy []string, groupBy []string, condition string)

Source from the content-addressed store, hash-verified

397//sql, err := LeftJoinSqlBuild(tables, on, fields, 1, 100, orderby, nil, "")
398//fmt.Println(sql, err)
399func LeftJoinSqlBuild(tables []string, on []map[string]string, fields map[string][]string, p, listRows int, orderBy []string, groupBy []string, condition string) (sql string, err error) {
400 if len(tables) < 2 || len(tables)-1 != len(on) {
401 err = errors.New("参数不规范:联合查询的数据表数量必须在2个或2个以上,同时表数量比on条件多一个")
402 return
403 }
404 var (
405 FieldSlice []string
406 StrOrderBy string
407 StrGroupBy string
408 StrCondition string
409 joinKV string
410 join = []string{tables[0]}
411 usedTables = []string{}
412 )
413 for table, field := range fields {
414 for _, f := range field {
415 FieldSlice = append(FieldSlice, strings.Trim(fmt.Sprintf("%v.%v", table, f), "."))
416 }
417 }
418 for index, table := range tables {
419 slice := strings.Split(strings.TrimSpace(table), " ")
420 if len(slice) == 1 {
421 slice = append(slice, slice[0])
422 }
423 usedTables = append(usedTables, slice[1])
424 if index > 0 {
425 on, joinKV = joinOn(slice[1], usedTables, on)
426 join = append(join, "left join "+table+" on "+joinKV)
427 }
428 }
429 if len(orderBy) > 0 {
430 StrOrderBy = " order by " + strings.Join(orderBy, ",")
431 }
432 if len(condition) > 0 {
433 StrCondition = " where " + condition
434 }
435 if len(groupBy) > 0 {
436 StrGroupBy = " group by " + strings.Join(groupBy, ",")
437 }
438
439 sql = fmt.Sprintf("select %v from %v %v %v %v limit %v offset %v", strings.Join(FieldSlice, ","), strings.Join(join, " "), StrCondition, StrGroupBy, StrOrderBy, listRows, (p-1)*listRows)
440 return
441}
442
443//只供LeftJoinSqlBuild创建SQL语句使用
444//@param table 需要左联查询的表

Callers 8

GetByIdMethod · 0.85
ListsMethod · 0.85
SearchByMysqlFunction · 0.85
GetCommentListMethod · 0.85
RecycleListMethod · 0.85
GetByIdMethod · 0.85
GetDocsByIdsMethod · 0.85

Calls 1

joinOnFunction · 0.85

Tested by

no test coverage detected