简体   繁体   中英

Using a joined column in a where condition

I have the following tables.The table class_students is related to the students table as it gets the student_id_column data from there.The examination_data has a column ( class_student_id_column ) that gets its data from the class_students table student_id_column column.

  1. students

student_id | student_names

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

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

3.........................some name

  1. class_students

class_students_id | student_id_column | class_year

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

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

  1. examination_data

examination_data_id | class_students_id_column | php | jquery | drawing

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

I want to update the table examination_data and to do that for a row ,i want my where statement to point to the student_id column.

To do that,i am trying this

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;

My update statement fails because there is no students.student_id column in the table examination_data .My other try is

  UPDATE  examination_data  SET examination_data.jquery = l FROM students INNER JOIN ON students.student_id=... WHERE students.student_id = 2;

but that hit a brick wall hard.

Is this even possible or will i have to have another student_id_column in the examination_data table?.

Use a sub query:

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

Try this query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM