diff --git a/chain.go b/chain.go index 4a422e4..27153be 100644 --- a/chain.go +++ b/chain.go @@ -145,6 +145,27 @@ func (this *Query) Join(join []string) *Query { this.join = append(this.join, join) return this } + +/** + * 左连接 + * 2023/08/10 + * gz + */ +func (this *Query) LeftJoin(table_name string, condition string) *Query { + this.join = append(this.join, []string{table_name, condition, "left"}) + return this +} + +/** + * 右连接 + * 2023/08/10 + * gz + */ +func (this *Query) RightJoin(table_name string, condition string) *Query { + this.join = append(this.join, []string{table_name, condition, "right"}) + return this +} + func (this *Query) Data(data string) *Query { this.data = append(this.data, data) return this @@ -290,15 +311,17 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { sql = helper.StringJoin(sql, " from ", table) if len(this.join) > 0 { + join_type := "left" for _, joinitem := range this.join { if len(joinitem) < 2 { continue } if len(joinitem) == 3 { - sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1]) + join_type = joinitem[2] } else { //默认左连接 - sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1]) + join_type = "left" } + sql = helper.StringJoin(sql, " ", join_type, " join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1]) } } if len(this.where) > 0 || len(this.where_or) > 0 { diff --git a/transaction_chain.go b/transaction_chain.go index 4e27911..2cc5f2d 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -147,6 +147,26 @@ func (this *TxQuery) Join(join []string) *TxQuery { this.join = append(this.join, join) return this } + +/** + * 左连接 + * 2023/08/10 + * gz + */ +func (this *TxQuery) LeftJoin(table_name string, condition string) *TxQuery { + this.join = append(this.join, []string{table_name, condition, "left"}) + return this +} + +/** + * 右连接 + * 2023/08/10 + * gz + */ +func (this *TxQuery) RightJoin(table_name string, condition string) *TxQuery { + this.join = append(this.join, []string{table_name, condition, "right"}) + return this +} func (this *TxQuery) Data(data string) *TxQuery { this.data = append(this.data, data) return this @@ -180,7 +200,7 @@ func (this *TxQuery) Clean() *TxQuery { return this } -//构造子查询 +// 构造子查询 func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { if this.dbname == "" && this.table == "" { return nil, errors.New("参数错误,没有数据表") @@ -278,7 +298,7 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { }, nil } -//获取表格信息 +// 获取表格信息 func (this *TxQuery) GetTableInfo(table string) (map[string]interface{}, error) { field := []string{ "COLUMN_NAME", //字段名