简体   繁体   English

使用具有附加条件的EXISTS获得最大行

[英]Obtaining a max row using EXISTS with additional condition/s

I have a table in MySQL database namely weight . 我在MySQL数据库中有一个表,即weight It can be viewed in the following snap shot. 可以在以下快照中查看它。

The weight table: 重量表:

重量 :

I need the following query. 我需要以下查询。

SELECT * 
FROM   weight w 
WHERE  w.weight_id IN (SELECT MAX(ww.weight_id) 
                       FROM   weight ww 
                       WHERE  ww.weight BETWEEN 0 AND 60);

Linguistically obtaining a max row from the weight table from among the rows where the weight column has a value between 0 and 60 (for example). weight表中从语言上获得最大行,例如weight weight列的值在0到60之间。


I want the same query using EXISTS() . 我想要使​​用EXISTS()进行相同的查询。 The following attempt is unsuccessful. 以下尝试失败。

SELECT * 
FROM   weight w 
WHERE  NOT EXISTS (SELECT 1 
               FROM   weight ww 
               WHERE  w.weight_id < ww.weight_id 
                      AND ww.weight BETWEEN 0 AND 60);

It returns all rows starting from the max row (if the BETWEEN condition ( AND ww.weight BETWEEN 0 AND 60 ) is omitted then, it returns a max from all the rows in the table but it is undesired. It should return a max row after applying a filter according to the BETWEEN condition). 它返回从最大行开始的所有行(如果省略了BETWEEN条件( AND ww.weight BETWEEN 0 AND 60 ),那么它将返回表中所有行的最大值,但它不是所希望的。它应该返回一个最大行根据BETWEEN条件应用过滤器后)。

A slightly modified version returns no rows. 稍作修改的版本将不返回任何行。

SELECT * 
FROM   weight w 
WHERE  EXISTS (SELECT 1 
               FROM   weight ww 
               WHERE  w.weight_id = ww.weight_id 
                      AND ww.weight BETWEEN 0 AND 60 
                      AND NOT EXISTS(SELECT 1 
                                     FROM   weight www 
                                     WHERE  ww.weight_id < www.weight_id)) 

Can I still have an alternative to use EXISTS() ? 我还能使用EXISTS()吗?

I'm using an ORM framework where a subquery is not supported in the FROM clause. 我使用的是ORM框架,其中FROM子句中不支持子查询。 Therefore, it would be far better, if a subquery in the FROM is excluded. 因此,如果排除FROM的子查询,那就更好了。

Generally speaking id should only be used as a row identifier and not compared using MAX() . 一般来说, id应该仅用作行标识符,而不能使用MAX()进行比较。

If you want one row with the greatest weight between two values: 如果要使一行的weight介于两个值之间:

  SELECT id, weight
    FROM weight
   WHERE weight BETWEEN 0 AND 60
ORDER BY weight DESC
   LIMIT 1

I would not use EXISTS in this instance. 我不会在这种情况下使用EXISTS

UPDATE 更新

If you cannot use LIMIT then: 如果您不能使用LIMIT则:

  SELECT id, weight
    FROM weight
   WHERE weight = (
    SELECT MAX(wi.weight)
      FROM weight wi 
     WHERE wi.weight BETWEEN 0 AND 60
         )

will return the same providing there is no dupe on MAX(weight) 会返回相同的值,前提是MAX(weight)上没有重复

If you insist on using EXISTS then you could try: 如果您坚持使用EXISTS则可以尝试:

 SELECT * 
   FROM weight w 
  WHERE NOT EXISTS (
   SELECT 1 
     FROM weight ww 
    WHERE w.weight < ww.weight 
      AND ww.weight BETWEEN 0 AND 60
        )
    AND w.weight BETWEEN 0 AND 60

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

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