2012-04-09 118 views
-1

有什麼不對這個查詢:SQL查詢給錯誤

SELECT count() as total_students 
FROM student_information; 

SELECT 
    (
     SELECT student_project_marks 
     from students_project 
     WHERE student_project_id=student_information.student_project_id 
    ) 
    + student_assignment_marks 
    + student_exam_marks AS total_marks 
FROM student_information 
ORDER BY total_marks DESC; 

UPDATE student_information 
SET (
     SELECT student_grade 
     FROM student_information 
     LIMIT 0.1*total_students 
     ORDER BY total_marks DESC 
    )="A"; 

蔭試圖選擇0.1*total_students數所獲得的總標記下令學生和更新他們的成績....前10%將被評爲A. 我得到錯誤: syntax error near '('

我有2個表:

create table if not exists student_information (
student_name varchar(80), 
student_roll_num int primary key, 
student_email varchar(64), 
student_assignment_marks int(2) check(student_assignment_marks<=30), 
student_exam_marks int(2) check(student_exam_marks<=50), 
student_project_id varchar(25), 
student_grade varchar(2) 
) 

create table if not exist students_project (
student_project_id varchar(25), 
student_project_title varchar(25), 
student_project_marks int(2) check(student_project_marks>=0 and student_project_marks<=20) 
) 
: 通過下面的查詢創建它們

通過student_project_idstudent_project表訪問項目中的標記。

現在我該如何根據總分數獎勵分數... 前10%必須被授予A,接下來的10%B等等...... 我知道如何計算總分數...... 我已經寫過這樣的查詢:

select student_roll_num, 
(SELECT student_project_marks 
    from students_project 
    WHERE student_project_id=student_information.student_project_id)+ 
student_assignment_marks+student_exam_marks as total_marks from student_information; 

它的工作原理。

+0

我不確定你可以在任何RDBMS中的LIMIT子句中進行數學運算。你不需要聲明變量來存儲值嗎? – 2012-04-09 15:54:42

+0

您的更新聲明有幾個問題。我不清楚你想要做什麼。 – 2012-04-09 15:58:23

+0

您還想通過_count_更新前10%的學生,當需要按年級考慮時 - 如果15%的學生獲得相同的最高成績,會發生什麼情況?你會懲罰一個'隨機'5%的人口。 – 2012-04-09 16:01:17

回答

2

這甚至不是大致正確的SQL。另外,還不清楚你是如何執行這些語句的(從控制檯,程序等)。

幾點意見,以幫助您在您的方式:

  1. SQL不會「記住」以前的聲明,從一個語句的結果下。因此,在第一條SQL語句中計算total_students與您嘗試在第三條語句中實際使用該值無關。同樣,在第三個查詢中,您在第二個查詢中派生total_marks的嘗試不可用。

  2. 如果內部查詢保證只爲student_information中的每一行生成一條記錄,那麼您的第二條語句纔有意義。我有理由相信,你試圖做的事情會更好地使用JOIN而不是子查詢來完成。

  3. 第三個查詢(UPDATE)是離SQL最遠的一個。 UPDATE對錶中的一列或多列進行操作。每列都分配一個新值。它操作的列必須用正確的標識符進行字面命名。您可以在等號的一側使用子查詢,但不能在左側使用子查詢(儘管您在這裏沒有任何理由使用)。要在其上進行操作的行集的條件屬於UPDATE語句末尾的WHERE子句,而不屬於任何類型的子查詢。

  4. 在猜測你的意圖,我想你也許需要從student_projects(假設每個學生都有幾個項目來考慮)SUM或AVG的檔次,有一個在你的任何查詢,沒有聚集。

+0

感謝您的信息...我不知道這... 其實我想根據總分數更新學生的成績......我可以計算出總分數(我創建了查詢正在執行並給出正確的結果),但問題在於選擇10%的學生並將其成績更新爲「A」......我很抱歉如果這個問題太過於愚蠢,但我是SQL新手...... – batman 2012-04-09 16:40:02

+0

請張貼表格的結構並描述它們如何相互關聯(即哪些表格與其他表格處於一對多關係)。 – 2012-04-09 16:41:58