简体   繁体   English

SQL查询:检索符合条件的列表

[英]SQL Query: Retrieve list which matches criteria

Sorry, I couldn't think of a better heading (or anything that makes sense). 抱歉,我想不出更好的标题(或任何有意义的标题)。

I have been trying to write a SQL query where I can retrieve the names of student who have the same level values as student Jaci Walker. 我一直试图编写一个SQL查询,以便在其中检索具有与学生Jaci Walker相同级别值的学生的姓名。

The format of the table is: 该表的格式为:

STUDENT(id, Lname, Fname, Level, Sex, DOB, Street, Suburb, City, Postcode, State)

So I know the Lname (Walker) and Fname (Jaci) and I need to find the Level of Jaci Walker and then output a list of names with the same Level. 所以我知道Lname (Walker)Fname (Jaci) ,我需要找到Jaci Walker的Level,然后输出具有相同Level的名称列表。

--Find Level of Jaci Walker
SELECT S.Fname, S.Name, S.Level
FROM Student S
WHERE S.Fname="Jaci" AND S.Lname="Walker"
GROUP BY S.Fname, S.Lname, S.Level;

I have figured out how to retrieve the Level of Jaci Walker , but don't know how to apply that to another query. 我已经弄清楚了如何检索Jaci Walker的Level,但是不知道如何将其应用于另一个查询。


Thankyou to everyone for your help, I'm just stuck on one little bit when adding the rest of the query into it. 谢谢大家的帮助,在将其余查询添加到其中时,我只停留了一点。

https://www.dropbox.com/s/3ws93pp1vk40awg/img.jpg https://www.dropbox.com/s/3ws93pp1vk40awg/img.jpg

SELECT S.Fname, S.LName
FROM Student S, Enrollment E, CourseSection CS, Location L
WHERE S.S_id = E.S_id
AND E.C_SE_ID = CS.C_SE_id
AND L.Loc_id = CS.Loc_ID
AND S.S_Level = (SELECT S.S_Level FROM Student S WHERE S.S_Fname = "Jaci" AND S.S_Lname = "Walker")
AND CS.C_SE_id = (SELECT CS.C_SE_id FROM CourseSection CS WHERE ?)
AND L.Loc_id = (SELECT L.Blodg_code FROM Location L WHERE L.Blodg_code = "BG");

try this : 尝试这个 :

SELECT S.Fname, S.Name, S.Level
FROM Student S
WHERE S.Level = 
    (SELECT Level
     FROM Student 
     WHERE Fname="Jaci" AND Lname="Walker"
    )

but you got to be sure to have only 1 student called Jaci Walker ... 但是您必须确保只有一个叫Jaci Walker的学生...

You can re-use your query as a subquery to find other entries with the same Level. 您可以将查询作为子查询重复使用,以查找具有相同级别的其他条目。

SELECT Fname, Name
FROM Student
WHERE Level = (
    SELECT Level FROM Student S WHERE S.Fname="Jaci" AND S.Lname="Walker")

You don't need to group your result. 您无需将结果分组。

Try this 尝试这个

Select Fname,Lname from Student 
where Level=(Select Level 
             from Student 
             where Fname='Jaci' AND Lname='Walker' );

I don't think you need group by and all for same... 我认为您不需要相同的分组依据...

Simply, 只是,

 SELECT S.Fname, S.Name, S.Level FROM Student S WHERE S.LEVEL LIKE (SELECT LEVEL FROM STUDENT WHERE Fname="Jaci" AND Lname="Walker"); 

Are you looking for same? 您是否在找一样的东西?

Try this: 尝试这个:

SELECT S.Fname, S.Name, S.Level FROM Student s 
WHERE Level = 
(SELECT TOP 1 Level FROM Student WHERE Fname = "Jaci" and Lname = "Walker")

If you don't use TOP 1, this query will fail if you have more than one "Jaci Walker" in your data. 如果您不使用TOP 1,则如果您的数据中有多个“ Jaci Walker”,则此查询将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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