MySQL在获取特定记录并使用限制时,小于或大于操作不起作用。

huangapple go评论46阅读模式
英文:

MySQL less than or greater than not working when getting specific records with limit

问题

I have a table as shown below with 5 records. I wanted to get rest of all records with amount less than 2000. Except last/latest two records.

invoiceId Date Amount
1156 2023-02-26 12:59 PM 28500.00
1157 2023-02-26 01:01 PM 300.00
1158 2023-02-26 01:02 PM 850.00
1159 2023-02-26 01:02 PM 29500.00
1160 2023-02-26 01:03 PM 850.00

This is the result I wanted(leaving latest 2 and get rest of all records amount below 2000):

invoiceId Date Amount
1157 2023-02-26 01:01 PM 300.00
1158 2023-02-26 01:02 PM 850.00
SELECT amount
from invoice
where invoiceDate LIKE '%2023-02-26%'
AND (amount < 2000)
ORDER BY invoiceId DESC
LIMIT 2,6

when I tried above query, it returns only one record 300 instead of expected 300 and 850. why?

But it works when tried with...

(amount > 0) or (amount > 300) or (amount > 500) -- as expected, 
(amount > 0), -- returns 300 and 850 and 28500.
(amount > 300), -- returns 850 and 28500.
(amount > 500), -- returns 850 and 28500.

But again it doesn't work when applied (amount > 20000), it returns null instead of expected 20000. Why?

英文:

I have a table as shown below with 5 records. I wanted to get rest of all records with amount less than 2000. Except last/latest two records.

invoiceId Date Amount
1156 2023-02-26 12:59 PM 28500.00
1157 2023-02-26 01:01 PM 300.00
1158 2023-02-26 01:02 PM 850.00
1159 2023-02-26 01:02 PM 29500.00
1160 2023-02-26 01:03 PM 850.00

This is the result I wanted(leaving latest 2 and get rest of all records amount below 2000):

invoiceId Date Amount
1157 2023-02-26 01:01 PM 300.00
1158 2023-02-26 01:02 PM 850.00
SELECT amount
from invoice
where invoiceDate LIKE &#39;%2023-02-26%&#39;
AND (amount &lt; 2000)
ORDER BY invoiceId DESC
LIMIT 2,6

when I tried above query, it returns only one record 300 instead of expected 300 and 850. why?

But it works when tried with...

(amount &gt; 0) or (amount &gt; 300) or (amount &gt; 500) -- as expected, 
(amount &gt; 0), -- returns 300 and 850 and 28500.
(amount &gt; 300), -- returns 850 and 28500.
(amount &gt; 500), -- returns 850 and 28500.

But again it doesn't work when applied (amount &gt; 20000), it returns null instead of expected 20000. Why?

答案1

得分: 1

递减需要排除的顶部记录数量可以解决此问题。

select * from invoice WHERE 
amount<2000 and invoiceId < (SELECT max(invoiceId)-1 FROM invoice)

但仍期望除此方法之外的智能答案。

英文:

Decrement required number of excluding records from top id can solve this.

select * from invoice WHERE 
amount&lt;2000 and invoiceId &lt; (SELECT max(invoiceId)-1 FROM invoice)

But still expecting a smart answer apart this method.

huangapple
  • 本文由 发表于 2023年2月26日 20:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定