Golang SQL query Errror: missing destination name Sum in *[]main.Tag

I’m trying to feed the results from the SQL Query into the Sum field of my struct however i’m met with this error despite their being a Sum Field in the struct.

The results from the query can range from 1-8 numbers depending on the date input in my REACT interface.

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type Tag struct {
	Sum              string `json:"Sum"`
	Stream_count     string `json:"stream_count"`
	Query_desc       string `json:"Query_Desc"`
	Query_start_date string `json:"Query_start_date"`
	Query_end_date   string `json:"Query_end_date"`
	Current_date     string `json:"Current_date"`
	Error_info       string `json:"Error_Info"`
}


func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	db, err := sqlx.Connect("mysql", "****")

	if err != nil {
		log.Print(err.Error())
		log.Print("Error Connecting to DB")
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}
	defer db.Close()

	date_1 := r.FormValue("date_1")
	date_2 := r.FormValue("date_2")
	time_1 := r.FormValue("time_1")
	time_2 := r.FormValue("time_2")

	var tag Tag

	Query := ("SELECT SUM(duration) Sum FROM sessions WHERE (app_id = '****' OR app_id ='****' OR app_id ='****' OR app_id ='****'OR app_id ='****') AND date(created ) between ? and ? and time(created ) between ? and ? AND (`media_src`='https://livestream.indemandradio.com/stream/index.m3u8' OR `media_src`='****' OR `media_src`='****' OR `media_src`='****'  OR `media_src`='****'  OR `media_src`='****') GROUP BY date(created)")

	err = db.Get(&tag, Query, date_1, date_2, time_1, time_2)

	log.Print(tag.Sum)

	tag.Query_desc = "Listener Hours"
	tag.Query_start_date = date_1
	tag.Query_end_date = date_2
	dt := time.Now()
	tag.Current_date = dt.Format("01-02-2006 15:04:05")
	if err != nil {
		tag.Error_info = err.Error()
	}
	
	j, err := json.Marshal(tag)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte((err.Error())))
		return
	}
	w.Write(j)

}

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":8081", nil))
}

Try this to check if works

var sum int
err = db.Get(&sum, Query, date_1, date_2, time_1, time_2)

if it works it seems sql gets confused about field name so maybe changing
SUM(duration) as Suma or something else could do the trick…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.