mirror of https://github.com/go-gorm/gorm.git
Add Joins support
This commit is contained in:
parent
d64f4825d7
commit
13302ba410
19
README.md
19
README.md
|
@ -886,6 +886,23 @@ rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
Date time.Time
|
||||||
|
Total int64
|
||||||
|
}
|
||||||
|
db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Scan(&results)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Joins
|
||||||
|
|
||||||
|
```go
|
||||||
|
rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
|
||||||
|
for rows.Next() {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Run Raw SQL
|
## Run Raw SQL
|
||||||
|
@ -970,7 +987,6 @@ db.Where("email = ?", "x@example.org").Attrs(User{RegisteredIp: "111.111.111.111
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
* Scan
|
|
||||||
* Support plugin
|
* Support plugin
|
||||||
BeforeQuery
|
BeforeQuery
|
||||||
BeforeSave
|
BeforeSave
|
||||||
|
@ -998,7 +1014,6 @@ db.Where("email = ?", "x@example.org").Attrs(User{RegisteredIp: "111.111.111.111
|
||||||
* Getter/Setter
|
* Getter/Setter
|
||||||
share or not? transaction?
|
share or not? transaction?
|
||||||
* Github Pages
|
* Github Pages
|
||||||
* Joins
|
|
||||||
* Includes
|
* Includes
|
||||||
* AlertColumn, DropColumn, AddIndex, RemoveIndex
|
* AlertColumn, DropColumn, AddIndex, RemoveIndex
|
||||||
|
|
||||||
|
|
4
do.go
4
do.go
|
@ -707,11 +707,11 @@ func (s *Do) havingSql() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) joinsSql() string {
|
func (s *Do) joinsSql() string {
|
||||||
return ""
|
return s.search.joinsStr + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) combinedSql() string {
|
func (s *Do) combinedSql() string {
|
||||||
return s.whereSql() + s.groupSql() + s.havingSql() + s.orderSql() + s.limitSql() + s.offsetSql()
|
return s.joinsSql() + s.whereSql() + s.groupSql() + s.havingSql() + s.orderSql() + s.limitSql() + s.offsetSql()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) createTable() *Do {
|
func (s *Do) createTable() *Do {
|
||||||
|
|
19
gorm_test.go
19
gorm_test.go
|
@ -1591,6 +1591,25 @@ func TestGroup(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJoins(t *testing.T) {
|
||||||
|
type result struct {
|
||||||
|
Name string
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{
|
||||||
|
Name: "joins",
|
||||||
|
Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}},
|
||||||
|
}
|
||||||
|
db.Save(&user)
|
||||||
|
|
||||||
|
var results []result
|
||||||
|
db.Table("users").Select("name, email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").Scan(&results)
|
||||||
|
if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" {
|
||||||
|
t.Errorf("Should find all two emails with Join")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NameIn1And2(d *DB) *DB {
|
func NameIn1And2(d *DB) *DB {
|
||||||
return d.Where("name in (?)", []string{"1", "2"})
|
return d.Where("name in (?)", []string{"1", "2"})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue