mysql查询语法错误

时间:2011-08-12 23:46:05

标签: php mysql sql joomla

我想在以下查询中仅提取maxscore =得分的测验,请告诉我语法有什么问题?

$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) ;

2 个答案:

答案 0 :(得分: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`

答案 1 :(得分: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" ) ;