2016-12-15 37 views
-1

我在GO中有一個查詢,它返回來自mysql的不同響應。Go語言查詢列參考不起作用

這裏是查詢:

SELECT DISTINCT 
     questions.id as question_id, 
     questions.question, 
     questions.priority, 
     questions.type 
    FROM questions 
    LEFT JOIN profile ON profile.user_id = ? 
    LEFT JOIN group ON group.user_id = profile.user_id 
    WHERE questions.status = 1 
    AND group.status = 1 
    AND questions.id NOT IN (SELECT DISTINCT question_id FROM answers WHERE user_id = profiles.user_id) 

當我MySQL的終端上運行,這是預期它返回什麼。但是當我嘗試在GO lang上運行它時,它有一個返回,它不應該被返回,因爲它已經在NOT IN子句中被過濾了,這些都是被回答的問題。當我試圖將profiles.user_id更改爲特定值時,它會返回預期的輸出。

我認爲使用列參數在GO中不起作用。如果我將users.profile更改爲特定變量,但還有其他查詢需要使用該功能才能實現預期輸出,這將是一個快速修復。

我嘗試使用stmt.Prepared語句和db.Query()有相同的結果

Go代碼:

query := " SELECT DISTINCT " + 
     "  questions.id as question_id, " + 
     "  questions.question, " + 
     "  questions.priority, " + 
     "  questions.type " + 
     " FROM questions " + 
     " LEFT JOIN profile ON profile.user_id = 1627 " + 
     " LEFT JOIN group ON group.user_id = profile.user_id " + 
     " WHERE questions.status = 1 " + 
     " AND group.status = 1 " + 
     " AND questions.id NOT IN (SELECT DISTINCT question_id FROM answers WHERE user_id = profiles.user_id); " 

stmt, err := db.Prepare(query) 
if err != nil { 
    checkErr(err) // proper error handling instead of panic in your app 
} 
defer stmt.Close() // Close the statement when we leave main()/the program terminates 

userId := json.userId 

args := []interface{}{} 
args = append(args, userId) 

start := time.Now() 
rows, err := stmt.Query(args...) 
elapsed := time.Since(start) 

if err != nil { 
    checkErr(err) // proper error handling instead of panic in your app 
} 

// Fetch rows 
for rows.Next() { 
    // get RawBytes from data 
    err = rows.Scan(&question.QuestionId, &question.Question, &question.Priority, &question.Type) 
    questions = append(questions, question) 
    if err != nil { 
     checkErr(err) // proper error handling instead of panic in your app 
    } 
} 
defer rows.Close() 
defer db.Close() 

有任何解決方法爲它工作?

感謝您的回覆

+0

請顯示驗證碼。 – Volker

回答

0

您可以使用問號'?'在地方查詢,你期望一個參數:

age := 27 
rows, err := db.Query("SELECT name FROM users WHERE age=?", age) 
if err != nil { 
     log.Fatal(err) 
} 
defer rows.Close() 
for rows.Next() { 
     var name string 
     if err := rows.Scan(&name); err != nil { 
       log.Fatal(err) 
     } 
     fmt.Printf("%s is %d\n", name, age) 
} 
if err := rows.Err(); err != nil { 
     log.Fatal(err) 
} 

此外,您還可以使用反勾'的代碼中的多行字符串(而不是串聯與+)。它會讓你的代碼更易於閱讀。

query := `SELECT DISTINCT 
    questions.id as question_id, 
    questions.question, 
    questions.priority, 
    questions.type 
FROM questions 
LEFT JOIN profile ON profile.user_id = 1627 
LEFT JOIN group ON group.user_id = profile.user_id 
WHERE questions.status = 1 
AND group.status = 1 
AND questions.id NOT IN (SELECT DISTINCT question_id FROM answers WHERE user_id = profiles.user_id); ` 
+0

是的,但我遇到的問題是引用where子句不工作的左連接列。 – banri16

+0

我明白了,但是在查詢字符串中沒有看到對參數的任何引用。 –

+0

(SELECT DISTINCT question_id FROM答案WHERE user_ID的= profiles.user_id)在此聲明我使用的配置文件USER_ID – banri16