从另一张表中按顺序排序

时间:2015-09-25 08:28:53

标签: sql sqlite

我有这两张桌子:

"CREATE TABLE IF NOT EXISTS school (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name CHAR NOT NULL" + ");"

Teacher

"CREATE TABLE IF NOT EXISTS teacher ( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "school_id NOT NULL, " + "name CHAT NOT NULL, " + "wage REAL NOT NULL," + "CONSTRAINT fk_school_id FOREIGN KEY (school_id) " + "REFERENCES school (id) ON DELETE CASCADE" + ");"

id, school_id, name, wage
1, 1, 'teacher1', 2000
2, 1, 'teacher2', 1500
3, 2, 'teacher3', 5600
4, 3, 'teacher4', 4300
5, 4, 'teacher5', 5000
6, 4, 'teacher6', 3500
7, 4, 'teacher7', 4000

我需要选择所有订购的学校,总薪水是教师的费用。

实施例: 教师

school.id, school.name
4, 'school 4'
2, 'school 2'
3, 'school 3'
1, 'school 1'

这应该是预期的结果:

($sub_str1) = $str1 =~ m{<strong>(.*?)_good_high</strong>};

1 个答案:

答案 0 :(得分:2)

SQL查询可以是:

SELECT s.id, s.name, SUM(t.wage)
FROM school AS s
JOIN teachers AS t on t.school.id = s.id
GROUP BY s.id, s.name
ORDER BY 3 DESC;