求TEACHER_TB中ID为1、2、3、4的教师基础数据总和
Got it, let's tackle this problem step by step. Based on your requirement and the provided table structure/data, here's the solution broken down clearly:
First, we need to clarify what "sum of basic data" refers to. From your table structure, the numeric fields are TEACHER_ID (primary key, summing this has no practical business meaning), SALARY_ID, and SUBJECT_ID. Below are solutions for different scenarios:
1. Sum numeric fields directly from TEACHER_TB
If you want to calculate the total of SALARY_ID and SUBJECT_ID for teachers with IDs 1-4, use this SQL query:
SELECT SUM(SALARY_ID) AS total_salary_id, SUM(SUBJECT_ID) AS total_subject_id FROM TEACHER_TB WHERE TEACHER_ID IN (1, 2, 3, 4);
Calculation Result
Using the provided data:
total_salary_id= 1 + 3 + 3 + 2 = 9total_subject_id= 2 + 4 + 8 + 6 = 20
2. Calculate actual salary total by joining the salary table
Since SALARY_ID is a foreign key linked to salary_tb, if "basic data sum" refers to the total salary amount of these teachers, you need to join the two tables (assuming salary_tb has SALARY_ID and SALARY_AMOUNT fields):
SELECT SUM(s.SALARY_AMOUNT) AS total_salary_amount FROM TEACHER_TB t JOIN SALARY_TB s ON t.SALARY_ID = s.SALARY_ID WHERE t.TEACHER_ID IN (1, 2, 3, 4);
Note
Since you haven't provided the specific data of salary_tb, this query is for reference only. You can substitute the actual salary data to get the final total.
内容的提问来源于stack exchange,提问作者kuwar

