简体   繁体   English

SELECT *在一列的MAX和第二个MAX ID之间,而另一列等于某个输入

[英]SELECT * in between MAX and second MAX id of a column WHERE another column equals a certain input

I have a table called orders with (ao) columns sid and q1_requested_by . 我有一个名为表orders与(AO)列sidq1_requested_by The sid column contains a unique id number, incrementing with every new input. sid列包含唯一的ID号,并随每个新输入递增。

Every now and then there is an input in column q1_requested_by with the value ORDERS PROCESSED . q1_requested_byq1_requested_by列中有一个值为ORDERS PROCESSED的输入。

I want to select everything from the table which falls in between the latest ÒRDERS PROCESSED input and the latest but one ORDERS PROCESSED input. 我想从表格中选择所有内容,该表格介于最新的ÒRDERS PROCESSED输入和最新的一次ORDERS PROCESSED输入之间。

I've tried the following, which doesn't work. 我尝试了以下方法,但不起作用。 It selects nothing. 它什么也没选择。

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

How could I solve this? 我该如何解决?

Hi you can just add limit to the second part of the query. 您好,您可以将限制添加到查询的第二部分。 Please try this: 请尝试以下方法:

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

First, you would use MySQL constructs: 首先,您将使用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

It will work :) 它将起作用:)

SELECT * FROM orders 
WHERE sid = (SELECT max(sid) FROM orders WHERE sid <>     
            (SELECT max(sid) FROM orders WHERE q1_requested_by = 'ORDERS PROCESSED'))

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

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