我有一个名为orders
的表,其中包含(a.o.)列sid
和q1_requested_by
。
sid
列包含唯一的ID号,每个新输入都会递增。
有时会在q1_requested_by
列中输入值ORDERS PROCESSED
。
我想从表格中选择介于最新ÒRDERS PROCESSED
输入和最新但只有ORDERS PROCESSED
输入之间的所有内容。
我尝试过以下操作,但不起作用。它什么都不选。
SELECT * FROM orders
WHERE sid < (SELECT max(sid) FROM orders
WHERE q1_requested_by = 'ORDERS PROCESSED')
AND sid > (SELECT TOP 2 sid FROM orders
WHERE q1_requested_by = 'ORDERS PROCESSED'
ORDER BY sid DESC)
ORDER BY sid DESC
我怎么解决这个问题?
答案 0 :(得分:1)
您好,您可以添加限制到查询的第二部分。请试试这个:
SELECT * FROM orders
WHERE sid < (SELECT max(sid) FROM orders WHERE q1_requested_by = 'ORDERS PROCESSED')
AND sid > (SELECT TOP 2 sid FROM orders
WHERE q1_requested_by = 'ORDERS PROCESSED' ORDER BY sid DESC LIMIT 1)
ORDER BY sid DESC
答案 1 :(得分:1)
首先,您将使用MySQL构造:
SELECT o.*
FROM orders o
WHERE o.sid < (SELECT max(sid)
FROM orders
WHERE q1_requested_by = 'ORDERS PROCESSED'
) AND
o.sid > (SELECT sid
FROM orders
WHERE q1_requested_by = 'ORDERS PROCESSED'
ORDER BY sid DESC
LIMIT 1, 1
)
ORDER BY o.sid DESC
答案 2 :(得分:0)
它会起作用:)
SELECT * FROM orders
WHERE sid = (SELECT max(sid) FROM orders WHERE sid <>
(SELECT max(sid) FROM orders WHERE q1_requested_by = 'ORDERS PROCESSED'))