mysql:(嵌套/sub)对同一表的查询

rkue9o1l  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(227)

我读过一些类似的帖子,但是我仍然很难找到几个查询来选择我需要的记录。有人想解决下面的两个问题吗?先谢谢你。

Slices Table  
 +--------+-----------+------------+------------+--------------+
 |   id   |   model   |   type     |   status   |  print_time  |
 +--------+-----------+------------+------------+--------------+
 |   01   |    01     |  Orient    |  Finished  |     100      |   # Orient Finished 
 |   02   |    01     |  Orient    |  Finished  |     120      |   # Estimate Finished 
 |   03   |    01     |  Estimate  |  Finished  |     70       |   # IGNORE  
 |   04   |    01     |  Estimate  |  Finished  |     90       |  
 |        |           |            |            |              |
 |   05   |    02     |  Orient    |  Finished  |     320      |   # Orient Finished
 |   06   |    02     |  Orient    |  Finished  |     240      |   # Estimate Slicing
 |   07   |    02     |  Estimate  |  Finished  |     260      |   # IGNORE  
 |   08   |    02     |  Estimate  |  Slicing   |     0        | 
 |        |           |            |            |              |
 |   09   |    03     |  Orient    |  Finished  |     560      |   # Orient Slicing
 |   10   |    03     |  Orient    |  Slicing   |     0        |   # Estimate Start
 |   11   |    03     |  Estimate  |  Start     |     0        |   # IGNORE  
 |   12   |    03     |  Estimate  |  Start     |     0        | 
 |        |           |            |            |              |
 |   13   |    04     |  Orient    |  Finished  |     480      |   # Orient Finished
 |   14   |    04     |  Orient    |  Finished  |     200      |   # Estimate Start
 |   15   |    04     |  Estimate  |  Start     |     0        |   # SELECT
 |   16   |    04     |  Estimate  |  Start     |     0        |   
 |        |           |            |            |              |
 |   17   |    05     |  Orient    |  Finished  |     560      |   # Orient Finished
 |   18   |    05     |  Orient    |  Finished  |     340      |   # Estimate Start
 |   19   |    05     |  Estimate  |  Start     |     0        |   # SELECT
 |   20   |    05     |  Estimate  |  Start     |     0        |   
 |        |           |            |            |              |
 |   21   |    06     |  Orient    |  Start     |     0        |   # Orient Start
 |   22   |    06     |  Orient    |  Start     |     0        |   # Estimate Start
 |   23   |    06     |  Estimate  |  Start     |     0        |   # IGNORE 
 |   24   |    06     |  Estimate  |  Start     |     0        |   
 +--------+-----------+------------+------------+--------------+

第一个查询:
我需要从相同的模型id中选择记录,其中type=estimate,status=start,只有当type=orient的所有相同模型id记录的status=finished时
第一个查询的所需结果:

+--------+-----------+------------+------------+--------------+
 |   id   |   model   |   type     |   status   |  print_time  |
 +--------+-----------+------------+------------+--------------+
 |   15   |    04     |  Estimate  |  Start     |     0        |
 |   16   |    04     |  Estimate  |  Start     |     0        |
 |   19   |    05     |  Estimate  |  Start     |     0        |
 |   20   |    05     |  Estimate  |  Start     |     0        |
 +--------+-----------+------------+------------+--------------+

第二个查询:
作为第二个类似的查询,我需要选择type=orient和status=finished的记录,仅当type=estimate的所有相同model id记录都具有status=start时,然后按打印时间排序,并仅显示打印时间最少的记录
第二个查询的所需结果:

+--------+-----------+------------+------------+--------------+
 |   id   |   model   |   type     |   status   |  print_time  |
 +--------+-----------+------------+------------+--------------+
 |   14   |    04     |  Orient    |  Finished  |     200      |
 |   18   |    05     |  Orient    |  Finished  |     340      |
 +--------+-----------+------------+------------+--------------+

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题