2011-08-12 165 views
1

我想拉只有在下面的查詢maxscore =分數的測驗,請告訴我什麼是錯誤的語法?mysql查詢語法錯誤

$database->setQuery('SELECT distinct qui.title AS name,' . 
     ' (SELECT GROUP_CONCAT(profiles.title) 
       FROM #__jquarks_users_profiles AS users_profiles 
       LEFT JOIN #__jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id 
       WHERE users_profiles.user_id = sessionWho.user_id) AS profile, ' . 
     ' (SELECT sum(score) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id 
       AND status <> -1) AS score,' . 
     ' (SELECT count(distinct(question_id)) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id) AS maxScore,' . 

      ' (SELECT count(id) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE status=-1 
       AND quizsession_id = quizSession.id) AS evaluate,' . 
' quizSession.finished_on,sessionWho.email' .  
     ' FROM #__jquarks_quizsession AS quizSession' . 
     ' LEFT JOIN #__jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id' . 
     ' LEFT JOIN #__jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id' . 
     ' LEFT JOIN #__jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id' . 
     ' LEFT JOIN #__jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id' . 
     ' LEFT JOIN #__jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id' . 
' LEFT JOIN #__jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id '. 

' WHERE sessionWho.user_id =' .$id AND score = maxScore) ; 
+0

所以有語法問題,不是一個SQL查詢本身? – sll

+1

拼寫語法。 – GolezTrol

+0

你可以直接去phpmyadmin並逐個檢查子查詢嗎? – WooDzu

回答

1
$database->setQuery(
    "SELECT * 
    FROM 
    (SELECT distinct qui.title AS name, 
      (SELECT GROUP_CONCAT(profiles.title) 
       FROM #__jquarks_users_profiles AS users_profiles 
       LEFT JOIN #__jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id 
       WHERE users_profiles.user_id = sessionWho.user_id) AS profile, 
      (SELECT sum(score) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id 
       AND status <> -1) AS score, 
      (SELECT count(distinct question_id) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id) AS maxScore, 
      (SELECT count(id) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE status=-1 
       AND quizsession_id = quizSession.id) AS evaluate, 
      quizSession.finished_on, 
      sessionWho.email  
     FROM #__jquarks_quizsession AS quizSession 
     LEFT JOIN #__jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id 
     LEFT JOIN #__jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id 
     LEFT JOIN #__jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id 
     LEFT JOIN #__jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id 
     LEFT JOIN #__jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id 
     LEFT JOIN #__jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id 
     WHERE sessionWho.user_id = " . $id . 
    ") 
    WHERE score = maxScore") ; 
+0

嗨感謝您的輸出,我再次收到..警告:無效的參數爲foreach()提供' – Tony77

+0

有人可以幫助嗎? – Tony77

+0

@TonyR:你有沒有在PHPMyAdmin中試過這個查詢(確保它至少可以)?如果查詢很好,那麼問題可能出現在您的PHP代碼中。但是你沒有發佈。編輯你的問題並添加PHP代碼。我在您發佈的代碼中看不到'foreach()'。 –

2

最後一部分,AND score = maxScore不要放在引號。這也應該是你的SQL字符串的一部分。

即使在應用於問題代碼的突出顯示中,您也可以很容易地看到這一點,但您的編輯器的熒光筆當然也應該讓它回味無窮。您是否使用NetBeans或Notepad ++等編輯器?

$database->setQuery('SELECT distinct qui.title AS name,' . 
     ' (SELECT GROUP_CONCAT(profiles.title) 
       FROM #__jquarks_users_profiles AS users_profiles 
       LEFT JOIN #__jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id 
       WHERE users_profiles.user_id = sessionWho.user_id) AS profile, ' . 
     ' (SELECT sum(score) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id 
       AND status <> -1) AS score,' . 
     ' (SELECT count(distinct(question_id)) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE quizsession_id = quizSession.id) AS maxScore,' . 

      ' (SELECT count(id) 
       FROM #__jquarks_quizzes_answersessions 
       WHERE status=-1 
       AND quizsession_id = quizSession.id) AS evaluate,' . 
' quizSession.finished_on,sessionWho.email' .  
     ' FROM #__jquarks_quizsession AS quizSession' . 
     ' LEFT JOIN #__jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id' . 
     ' LEFT JOIN #__jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id' . 
     ' LEFT JOIN #__jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id' . 
     ' LEFT JOIN #__jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id' . 
     ' LEFT JOIN #__jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id' . 
' LEFT JOIN #__jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id '. 

' WHERE sessionWho.user_id =' .$id . ' AND score = maxScore') ; 

PS:你的表名實際上是否包含#?也許你應該使用圍繞這些表名反引號,然後,像:

LEFT JOIN `#__jquarks_users_profiles` 
+0

感謝您的幫助GolezTrol,會好心幫助,現在我得到和錯誤說明:警告:爲foreach提供的無效參數() – Tony77

+0

輸出查詢的代碼:'echo'

'; 回聲 「」; $ row = $ database-> loadRowList(); 的foreach($行作爲$ valuearray) { 回波 ''; 的foreach($ valuearray爲$場) { 回聲「​​$字段」; } 回聲 「 」; } 回聲「
測驗題目分數最大得分未答覆的完成了
」; >'' – Tony77

+0

和score''maxScore'不是字段,但評估列,他們不能在'WHERE'條款 –