我正在参加斯坦福大学的在线数据库课程,并被困在练习中。 Here is the database。现在的问题是:SQL查询选择仅匹配特定条件的所有行
Find names and grades of students who only have friends in the same grade.
这里是我的尝试:
/* Select all highschoolers and their grades... */
SELECT h1.name, h1.grade
FROM Highschooler h1
/* ...where their grades are equal to all their friends' grades */
WHERE h1.grade = ALL(
/* Select friends' grades */
SELECT h2.grade
FROM Highschooler h2
JOIN Friend f
ON f.ID1 = h1.ID
AND f.ID2 = h2.ID
);
而且我得到这个错误:
Query failed to execute: near 'ALL': syntax error
我检查我的SQL与online syntax checker,并把它传递。我错过了什么?
这是SQL Server吗?或者mysql?你没有指定一个DBMS ..我实际上用IN或类似的构造替换ALL。 – Leptonator
什么是错误信息? – JassyJov
@Lemonator正确。要小心,sql有不同的方言,所以如果你是在互联网或书籍上的例子,请确保你使用的是相同的dtb软件。 – Divisadero