mysql - 查询返回空集

标签 mysql

非常感谢任何帮助。

我有一家餐 table 医院:

Nurse + Year + No.Patients<br/> A001 |2000 | 23 <br/> A001 |2001 | 30 <br/> A001 |2002 | 35 <br/> <br/> B001 |2000 | 12 <br/> B001 |2001 | 15 <br/> B001 |2002 | 45 <br/> <br/> C001 |2000 | 50 <br/> C002 |2001 | 59 <br/> C003 |2002 | 69 <br/> etc

<p>What I am trying to do is work out which nurse had the greatest increase of patients for the years 2000 - 2002.</p> <p>Clearly B001 did as her patients increased from 12 to 45 and increase of 33 and what I am trying to produce is the result <code>B001 | 33</code>.</p> <p>This is what I have so far:</p> <pre><code>select a.nurse,a.nopats from hospital as a join ( select nurse,max(nopats)-min(nopats) as growth from hospital where year between 2000 and 2002 group by nurse ) as s1 on a.nurse = s1.nurse and a.nopats = s1.growth where year between 2000 and 2002; </code></pre> <p>but all I get returned is an empty set.</p>

I think I need an overall max(nopats)加入后。

这里的任何帮助都会很棒。

谢谢!

最佳答案

试试这个:

SELECT nurse, (max(nopats) - min(nopats)) AS growth
  FROM hospital
 WHERE year BETWEEN 2000 AND 2002
 GROUP BY nurse
 ORDER BY growth DESC
LIMIT 1;

结果:B001 |由于 LIMIT 1,为 33;如果您想要更多结果,请将其保留。

关于mysql - 查询返回空集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1148930/

相关文章:

android - MySQLNonTransientConnectionException : Could not create connection to database server

java - Hibernate级联子对象不在查找表中存储父表的ID

java - 在 JPA MySql 中存储与时区无关的时间的最佳方法

php - 为什么我无法向mysql服务器插入数据?

mysql - 如何在 MySQL 数据库中定义一个 append-only 表?

mysql - 如何左加入旧ID

php - 我怎样才能改进我的查询不得到 "PHP Fatal error: Allowed memory size"(在 laravel 5.2 中排序和分页)

php - 使用 PHP 生成 XML 文件

php - 为什么将posted变量打印到html代码中会导致XSS?

mysql - 将数据框中的行存储在数据库中