组根据另一个字段中的2个相同数据来组合一个字段

时间:2019-03-12 09:29:08

标签: mysql sql concat group-concat

给出该表,

+----+------------------+-----------+----------+
| id | Doctor name      | firsttime | lasttime |
+----+------------------+-----------+----------+
|  1 | Dr. Abdurahman   | 12:00:00  | 21:00:00 |
|  2 | Dr. Sultan Hamid | 12:00:00  | 21:00:00 |
|  3 | Dr. Susanti      | 12:00:00  | 21:00:00 |
|  4 | Dr. Siparman     | 12:00:00  | 21:00:00 |
|  5 | Dr. Ramah        | 12:00:00  | 21:00:00 |
|  6 | Drs. Susanto     | 13:00:00  | 22:00:00 |
|  7 | Dr. Budiarjo     | 13:00:00  | 22:00:00 |
|  8 | Dr. Antonius     | 13:00:00  | 22:00:00 |
|  9 | Dr. Wahid Bahyu  | 13:00:00  | 22:00:00 |
+----+------------------+-----------+----------+

expected
+----+--------------------------------------------+-----------+----------+
| id | Doctor name                                | firsttime | lasttime |
+----+--------------------------------------------+-----------+----------+
|  1 | Dr. Abdurahman, Dr. Sultan Hamid, etc      | 12:00:00  | 21:00:00 |
|  2 | Drs. Susanto, Dr. Budiarjo, etc            | 13:00:00  | 22:00:00 |
+----+--------------------------------------------+-----------+----------+

我期望的是根据第一次和最后一次选择表格,并在Doctor name上使用组concat。因此,如果有相同的firstdate和lastdate,它将被包含在concat组中

注意:第一个日期和最后一个日期是随机的,请忽略等等,我希望它是医生的全名

2 个答案:

答案 0 :(得分:2)

使用GROUP_CONCAT

SELECT firstdate,lastdate,
    GROUP_CONCAT(doctorname)
FROM
    table group by firstdate,lastdate

答案 1 :(得分:1)

或者您可以使用STRING_AGG()函数:

package main

import (
    "fmt"
    "net/http"
   "github.com/gorilla/sessions"
)

var (
    // key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
    key = []byte("super-secret-key")
    store = sessions.NewCookieStore(key)
)

func secret(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Check if user is authenticated
    if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
        http.Error(w, "Forbidden", http.StatusForbidden)
        return
    }

    // Print secret message
    fmt.Fprintln(w, "The cake is a lie!")
        fmt.Fprintln(w, session.Values["foo"])
}

func login(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Authentication goes here

    // Set user as authenticated
  session.Values["authenticated"] = true
  session.Values["foo"] = "bar"
  session.Values[42] = 43
    session.Save(r, w)

fmt.Fprintln(w, session.Values["authenticated"])
fmt.Fprintln(w, session.Values["foo"])
fmt.Fprintln(w, session.Values["2"])
}

func logout(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Revoke users authentication
    session.Values["authenticated"] = false
        session.Values["foo"] = ""
        session.Values[42] = ""
    session.Save(r, w)

// prevent caching
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Accel-Expires", "0")


}

func main() {
    http.HandleFunc("/secret", secret)
    http.HandleFunc("/login", login)
    http.HandleFunc("/logout", logout)

    http.ListenAndServe(":8000", nil)
}