我正在尝试解决课程的问题,我之前在以下链接中看到了问题:SQL - How to find the person with the highest grade
我看到了提供的答案,但注意到它不能用来帮助我,因为它使用了我不允许使用的连接和顺序,我不能评论这个问题,因为我是新的,所以我再次发布这个问题。
使用以下3个表格查找成绩最高的人员的姓名
People (id, name, age, address)
---------------------------------------------------
p1 | Tom Martin| 24 | 11, Wall Street, New York, NY
p2 | D.J. Tanner | 33 | 1882, Gerard Street, San Francisco, CA
p3 | Lisa Simpson| 40 | 742, Evergreen Terrace, Springfield, IL
---------------------------------------------------
Courses (cid, name, department)
---------------------------------------------------------
c1 | Systematic Torture | MATH
c2 | Pretty Painful | CS
c3 | Not so Bad | MATH
c4 | Big Data | CS
---------------------------------------------------------
Grades (pid, cid, grade)
---------------------------------------------------
p1 | c1 | 3.5
p2 | c3 | 2.5
p3 | c2 | 4.0
p3 | c4 | 3.85
p2 | c4 | 3.5
p1 | c4 | 2.5
---------------------------------------------------
我当前的查询:
SELECT DISTINCT People.name
FROM Grades AS g1, Grades AS g2, people AS p1
WHERE g1.cid = 'c4'
AND g2.cid = 'c4'
AND p1.id =- g1.pid
AND g1.grade > g2.grade
我的结果:
Lisa Simpson
Tom Martin
我只需要Lisa Simpson并坚持寻找解决方法。
以下内容无法使用:
COUNT
MAX
MIN
SUM
GROUP BY
HAVING
INNER JOIN
LEFT JOIN
RIGHT JOIN
Anything JOIN
EXISTS
NOT EXISTS
ORDER BY
TOP
只能使用:
SELECT
FROM..AS
WHERE
UNION
INTERSECT
EXCEPT
CREATE VIEW ... AS ...
Arithmetic operators like < > <= ==
AND
OR
基本上他不会让我们使用我们在课堂上没有涉及的任何内容
答案 0 :(得分:2)
您将依赖大于所有运算符来执行此操作:
class Xml
{
//Function for Read Xml file and store into Sql table
public void ReadXmlFileAndInsertIntoTable(string client, string xmlpath, string Connectionstring)
{
try
{
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
// XmlReader xmlFile;
string sql = null;
int product_ID = 0;
string Product_Name = null;
double product_Price = 0;
connetionString = Connectionstring;
connection = new SqlConnection(connetionString);
//xmlFile = XmlReader.Create(xmlpath, new XmlReaderSettings());
//ds.ReadXml(xmlFile);
string xmlFile = xmlpath;
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(xmlFile))
{
xmlDoc.Load(xmlFile);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\n\t Xml File Read Successfully");
XmlNode node = xmlDoc.DocumentElement.FirstChild;
XmlNodeList nodelist = node.ChildNodes;
//for (int j = 0; j < nodelist.Count; j++)
// Console.WriteLine("{0}", nodelist[j].InnerText);
ds.ReadXml(xmlFile);
}
else
{
Console.WriteLine("The file {0} could not be located", xmlFile);
}
connection.Open();
int i = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
command = new SqlCommand(sql, connection);
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
}
connection.Close();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\n\t Data Inserted Successfully...!!!");
}
catch(Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
}
}
}
//Main Function
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\t=====================================================================");
Console.WriteLine("\t\t My Application");
Console.WriteLine("\n\t=====================================================================");
Console.WriteLine("\t 1. Try Simple XML Data Copy and store in Table");
Console.WriteLine("\t 2. Exit Tool");
Console.Write("\n\t Please Enter Your Response :- ");
int n = Convert.ToInt32(Console.ReadLine());
switch (n)
{
case 1:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\t\t Enter Client Name :- ");
string client = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\t\t Enter XML Path :- ");
var xmlpath = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\n\t\t Enter Connctionstring :- ");
var Connectionstring = Console.ReadLine();
Xml xml = new Xml();
xml.ReadXmlFileAndInsertIntoTable(client, xmlpath, Connectionstring);
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n\t\t Please enter valid response...!!!");
break;
}
Console.ReadLine();
}
}
}
这是在没有连接,最大和顺序的情况下进行的。
修改强>: 关于使用限制1的说明
限制的使用是因为我正在进行比较select name from people
where id =
-- this subquery returns the id of the person with the maximum grade
(select g.pid
from grade g
where
-- this is an alternative to implement the max function
-- It means: where grade is greater then every grade that it is not itself.
g.grade > all (select g2.grade from grade g2 where g2.grade <> g.grade)
limit 1
)
,而 某些内容需要与id相同的类型。例如,我无法将int类型的事物与整数列表进行比较。如果我没有使用限制,子查询将返回一个列表,我们有两个最大数字。
答案 1 :(得分:0)
SELECT p.name
FROM grades a
JOIN
( SELECT MAX(grade) grade
FROM grades
) b
ON b.grade = a.grade
JOIN people p
ON p.id = a.pid;