2014-01-24 199 views
3

我的應用程序生成此query.Can有人告訴什麼是錯的查詢語法不正確

 SELECT task_id, 
    Count(*) Unread 
    FROM discussion 
    WHERE NOT EXISTS (SELECT * 
         FROM discussion_read 
         WHERE discussion.discussion_id = 
         discussion_read.discussion_id 
         AND discussion_read.user_id = ? 
         AND discussion.task_id IN 
          ((SELECT TOP 1025 task.task_id 
          FROM task 
          WHERE 
          ((task.task_code = 'A3090') 
          AND (task.proj_id = 6511))) 
               ORDER BY task.task_id ASC) 
      ) 
    GROUP BY task_id 
+0

我試圖保存我的視圖,使用執行查詢時出現此錯誤我很好。我的朋友告訴我,你不能使用ORDER BY查看查詢 - 我想這是有道理的。 – Nacht

回答

0

您的訂單由需求)下面試試

SELECT task_id, 
Count(*) Unread 
FROM discussion 
WHERE NOT EXISTS (SELECT * 
        FROM discussion_read 
        WHERE discussion.discussion_id = 
        discussion_read.discussion_id 
        AND discussion_read.user_id = ? 
        AND discussion.task_id IN 
         ((SELECT TOP 1025 task.task_id 
         FROM task 
         WHERE 
         ((task.task_code = 'A3090') 
         AND (task.proj_id = 6511)) 
              ORDER BY task.task_id ASC)) 
     ) 
GROUP BY task_id 
+1

@ BarbC.Goldstein:這是必要的,因爲在選擇查詢中使用了「TOP」關鍵字。沒有'Order By'的'Top'回退隨機的前25行 – praveen

0

試試這個: -

SELECT task_id, 
Count(*) Unread 
FROM discussion 
WHERE NOT EXISTS (SELECT * 
        FROM discussion_read 
        WHERE discussion.discussion_id = 
        discussion_read.discussion_id 
        AND discussion_read.user_id = ? 
        AND discussion.task_id IN 
         (SELECT TOP 1025 task.task_id --Remove Extra braces 
         FROM task 
         WHERE 
         task.task_code = 'A3090' 
         AND task.proj_id = 6511 
         ORDER BY task.task_id ASC -- It should be for Select Top statement 
         ) 

     ) 
GROUP BY task_id