Mysql 使用;“订购”;更改查询结果
我最近正在学习的SQL教程 给出的表格定义非常简单:Mysql 使用;“订购”;更改查询结果,mysql,sql,Mysql,Sql,我最近正在学习的SQL教程 给出的表格定义非常简单: stops(id, name) route(num, company, pos, stop) 这里,stop属性指的是在stops表中提供名称的站点的id 如果你看问题#10,我提出了以下解决方案,网站指出该解决方案产生了正确的结果: SELECT distinct a.num, a.company, bstops.name, e.num, e.company FROM route a JOIN route b ON (a.compa
stops(id, name)
route(num, company, pos, stop)
这里,stop
属性指的是在stops
表中提供名称的站点的id
如果你看问题#10,我提出了以下解决方案,网站指出该解决方案产生了正确的结果:
SELECT distinct a.num, a.company, bstops.name, e.num, e.company FROM route a
JOIN route b ON (a.company = b.company AND a.num = b.num)
JOIN stops bstops ON bstops.id = b.stop
JOIN
(SELECT c.company, c.num, d.stop as d_stop FROM route c
JOIN route d ON (c.company = d.company AND c.num = d.num)
WHERE c.stop = 213) e
ON e.d_stop = b.stop
WHERE a.stop = 53
在这里,我已经知道Craiglockhart
的id
是53
,而Sighthill
的id
是213
。到目前为止一切都很好
但是如果我在查询中添加一个orderbyname
或orderbybstops.name
,结果会发生变化,并且会找到更多的结果:
SELECT distinct a.num, a.company, bstops.name, e.num, e.company FROM route a
JOIN route b ON (a.company = b.company AND a.num = b.num)
JOIN stops bstops ON bstops.id = b.stop
JOIN
(SELECT c.company, c.num, d.stop as d_stop FROM route c
JOIN route d ON (c.company = d.company AND c.num = d.num)
WHERE c.stop = 213) e
ON e.d_stop = b.stop
WHERE a.stop = 53 ORDER BY name
具体来说,例如,伦敦路现在有8排,而不是4排
为了理解这些结果,我一直在使用不同的查询。除了排序之外,
ORDER BY
还应该更改查询的实际结果,这有什么原因吗?您可以在查询中使用limit
关键字来限制返回的记录数。我想他们正在添加类似的内容
... LIMIT 50 ...
为避免返回太多的记录,请单击要提交的查询
然后,显示的前50条记录可能因不同的ORDER BY表达式而有所不同,因为先对记录进行排序,然后对排序结果进行限制
假设您有这个表
:
Id Name
--+-------
0 |Andreas
1 |Lurker
2 |Gordon
3 |John
那么
当
除非苏格兰的公共汽车在同一个车站停下来,不管行驶方向如何,否则我认为SQLZOO提供的答案是错误的 我想你可以用这个问题来模仿他们的答案
SELECT a.num first_bus
, a.company first_company
, s1.name departing_from
, s2.name interchange
, d.num second_bus
, d.company second_company
, s3.name arriving_at
FROM route a
JOIN route b
ON b.num = a.num
AND b.pos <> a.pos
JOIN route c
ON c.stop = b.stop
JOIN route d
ON d.num = c.num
AND d.pos <> c.pos
JOIN stops s1
ON s1.id = a.stop
JOIN stops s2
ON s2.id = b.stop
JOIN stops s3
ON s3.id = d.stop
WHERE s1.name = 'Craiglockhart'
AND s3.name = 'Sighthill';
我假设他们在后台添加了一个“limitby”-在这两种情况下返回的行数都是50,但是不同的顺序导致不同的记录显示为前50行records@lurker . . . 执行
选择count(*)
查看实际应返回多少行。这个数字可能大于50。@Andreas哦,对了!好悲伤,谢谢,这是一个很好的收获。戈登,我试着用你的count(*)
来验证安德烈亚斯的理论。一切都很好。谢谢,那太傻了。谢谢,你抓住了这个问题。当我得到所谓的“正确”结果时,我忘了考虑他们在该网站上的内置限制。我的理智和SQL世界秩序已恢复。:)是的,我认为你对SQLZOO的看法是正确的。这只是一个练习,所以他们对所陈述的问题采取了一种简单化的观点,而且,在我看来,这在逻辑上并不总是完全正确的答案。在这种情况下,他们没有解释pos
属性,也没有表示应该考虑它。
SELECT * FROM table ORDER BY Name LIMIT 2;
Id Name
--+-------
0 |Andreas
2 |Gordon
SELECT a.num first_bus
, a.company first_company
, s1.name departing_from
, s2.name interchange
, d.num second_bus
, d.company second_company
, s3.name arriving_at
FROM route a
JOIN route b
ON b.num = a.num
AND b.pos <> a.pos
JOIN route c
ON c.stop = b.stop
JOIN route d
ON d.num = c.num
AND d.pos <> c.pos
JOIN stops s1
ON s1.id = a.stop
JOIN stops s2
ON s2.id = b.stop
JOIN stops s3
ON s3.id = d.stop
WHERE s1.name = 'Craiglockhart'
AND s3.name = 'Sighthill';
SELECT a.num first_bus
, a.company first_company
, s1.name departing_from
, s2.name interchange
, d.num second_bus
, d.company second_company
, s3.name arriving_at
FROM route a
JOIN route b
ON b.num = a.num
AND b.pos > a.pos
JOIN route c
ON c.stop = b.stop
JOIN route d
ON d.num = c.num
AND d.pos > c.pos
JOIN stops s1
ON s1.id = a.stop
JOIN stops s2
ON s2.id = b.stop
JOIN stops s3
ON s3.id = d.stop
WHERE s1.name = 'Craiglockhart'
AND s3.name = 'Sighthill';