我在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()
有任何解決方法爲它工作?
感謝您的回覆
請顯示驗證碼。 – Volker