英文:
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 '%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?
答案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<2000 and invoiceId < (SELECT max(invoiceId)-1 FROM invoice)
But still expecting a smart answer apart this method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论