开发者

How to filter MySQL SELECT by an aggregate function?

开发者 https://www.devze.com 2022-12-23 16:46 出处:网络
SELECT 开发者_运维技巧h11, HA11 FROM florin.h11 WHERE (3>d1 AND 3<d2) OR (3>d1 AND 3=d2 AND id=MAX(id))
SELECT 开发者_运维技巧h11, HA11
FROM florin.h11
WHERE (3>d1 AND 3<d2) OR (3>d1 AND 3=d2 AND id=MAX(id))
UNION (3=d1 AND 3<d2 AND id=MIN(id));

Here a screenshot of my table stucture:

How to filter MySQL SELECT by an aggregate function?


I think what you would like to do is something like this:

SELECT h11, HA11
FROM florin.h11 
WHERE (3>d1 AND 3<d2) 
   OR (3>d1 AND 3=d2 AND id = (SELECT MAX(t2.id) 
                               FROM florin.h11 AS t2))
   OR (3=d1 AND 3<d2 AND id = (SELECT MIN(t3.id)
                               FROM florin.h11 AS t3));


First wrong thing is

  • Please mention if, florin is database and h11 is table.

If florin is table then write only "select h11,HA11 from florin", don't write column name.

Second wrong thing is

  • Union operator is used to join results of two query. For more info surf www.w3school.com

So, fire query like

select h11, HA11 
from florin 
where (3 > d1 and 3 < d2) or (3 > d1 and 3 = d2 and id = MAX(id))

union 

select h11, HA11 
from florin 
where (3 = d1 and 3 < d2 and id = MIN(id))

Here may this query doesn't return desired result but this was my imagine that you want such type of query.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号