在where条件中使用连接列

时间:2013-07-24 17:43:40

标签: mysql

我有以下表格。表格class_studentsstudents表格相关,因为它从那里获取student_id_column数据。examination_data有一列({ {1}})从class_student_id_columnclass_students列获取数据。

  1. student_id_column
  2.   

    student_id | student_names

         

    1 ........................... ed

         

    2 .......................... bravo

         

    3 .........................某个名字

    1. students
    2.   

      class_students_id | student_id_column | class_year

           

      1 ................................... 2 .......... .................... 2000

           

      2 ................................... 3 .......... .................... 2000

      1. class_students
      2. examination_data_id | class_students_id_column | php | jquery |绘制

        1 ......................................... 1 .... ............................. 90 ..... 68 ............ 89 1 ......................................... 2 ....... .......................... 64 ..... 89 ............ 99

        我想要更新表examination_data并为行执行此操作,我希望我的where语句指向examination_data列。

        要做到这一点,我正在尝试这个

        student_id

        我的更新语句失败,因为表SELECT examination_data.examination_data_id, examination_data.class_students_id_column, class_students.student_id_column, students.student_id, examination_data.php, examination_data.jquery, examination_data.drawing FROM examination_data LEFT JOIN class_students ON (examination_data.class_students_id_column=class_students.class_students_id) LEFT JOIN students ON (students.student_id=class_students.student_id_column) UPDATE examination_data SET examination_data.jquery = 96 WHERE students.student_id = 2; 中没有students.student_id列。我的另一个尝试是

        examination_data
        但是那很难打砖墙。

        这是否可能,或者我必须在 UPDATE examination_data SET examination_data.jquery = l FROM students INNER JOIN ON students.student_id=... WHERE students.student_id = 2; 表中另外有student_id_column吗?

2 个答案:

答案 0 :(得分:2)

使用子查询:

UPDATE
  examination_data
SET
  jquery = 96
WHERE
  class_students_id_column = (
    SELECT class_students_id FROM class_students WHERE student_id_column = 2
  )

答案 1 :(得分:0)

尝试此查询

UPDATE  examination_data  SET examination_data.jquery = 96 WHERE
examination_data.class_students_id_column =(
SELECT class_students_id FROM class_students WHERE student_id_column = 2);