我正在修改我的考试,我对SQL查询有点挣扎..
我的任务是:
编写SQL查询以列出所有客户端的全名 参加艾伦摩尔的课程,按姓氏的字母顺序排列(即 Lname列)
客户端
Cid Fname Lname Fitness
C129 Julie Summer 1
C525 Max Hedrum 3
C628 John Long 3
C772 Warren Peace 2
C829 Anna Heart 2
程序
Code Title Fitness
AR02 Aerobics 2
EN99 Endurance 3
TU10 Tune-Up 1
UB01 Upper-Body 2
YG02 Yoga 1
员工
Sid Fname Lname Position Salary
S09 Jenny Sayer Psychologist 23500
S22 Allen Moore Instructor 21500
S28 Polly Purves Instructor 19000
S35 Jim Shoe Instructor 18000
S55 Mark Spencer Manager 25500
类
Code Sid Cid
AR02 S35 C772
EN99 S22 C525
TU10 S35 C129
UB01 S28 C628
YG02 S22 C829
YG02 S22 C12
是这样的:
SELECT Cliente.Fname, Cliente.Lname
FROM Staff, Class, Cliente
WHERE Staff.Sid = Class.Sid AND Staff.Fname = "Allen" AND Staff.Lname = "Moore
AND Class.Cid = Cliente.Cid
GROUP BY Cliente.lName ASC;
谢谢!
答案 0 :(得分:2)
select c.fname + ' ' + c.lname
from Client c
inner join Class cl
on c.cid = cl.cid
inner join Staff s
on s.sid = cl.sid
where s.fname = 'allen'
and s.lname = 'moore'
order by c.lname
答案 1 :(得分:2)
为JOIN使用不同的别名和更流行的约定,但您的查询也应该有效。此外,ORDER BY升序是默认值,因此ASC是可选的......
SELECT CLNT.Fname, CLNT.Lname
FROM Class AS CLS
INNER JOIN Client AS CLNT ON CLS.Cid = CLNT.Cid
INNER JOIN Staff AS S ON CLS.Sid = S.Sid
WHERE S.Fname = 'Allen'
AND S.Lname = 'Moore'
ORDER BY CLNT.Lname;
答案 2 :(得分:1)
尝试使用JOIN ... ON子句。
答案 3 :(得分:1)
您的查询中的GROUP BY必须去,您不必聚合任何东西。我更喜欢连接的SQL92语法:
SELECT
Client.Fname, Client.Lname
FROM
Staff
INNER JOIN
Class ON Staff.Sid = Class.Sid AND Staff.Fname = "Allen" AND Staff.Lname = "Moore"
INNER JOIN
Client ON Class.Cid = Cliente.Cid
ORDER BY Cliente.lName ASC;
答案 4 :(得分:1)
由于实际上没有使用其他表中的信息,为什么要加入?
SELECT FName, LName
FROM Client
WHERE
Cid IN (
SELECT Cid
FROM Class
WHERE Sid = (
SELECT Sid FROM Staff WHERE FName = 'Allen' AND LName = 'Moore'
)
)
ORDER BY LName ASC;