旋转表数据而不更新数据

发布于 2024-09-19 02:23:07 字数 320 浏览 7 评论 0原文

 SELECT * FROM `your_table` LIMIT 0, 10 

->这将显示第一个 1,2,3,4,5,6,7,8,9,10

 SELECT * FROM `your_table` LIMIT 5, 5 

->这将显示记录 6,7,8,9,10

我想显示数据 2, 3,4,5,6,7,8,9,10,1 和 第二天 3,4,5,6,7,8,9,10,1,2 后天 4,5,6,7,8,9,10,1,2,3

是否可以不更新此表的任何数据???

 SELECT * FROM `your_table` LIMIT 0, 10 

->This will display the first 1,2,3,4,5,6,7,8,9,10

 SELECT * FROM `your_table` LIMIT 5, 5 

->This will show records 6, 7, 8, 9, 10

I want to Show data 2,3,4,5,6,7,8,9,10,1 and
next day 3,4,5,6,7,8,9,10,1,2
day after next day 4,5,6,7,8,9,10,1,2,3

IS IT POSSIBLE with out updating any data of this table ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2024-09-26 02:23:07

您可以使用 UNION 语法来完成此操作:

SELECT * FROM `your_table` LIMIT 5, 5 UNION SELECT * FROM `your_table`

这将首先选择限制范围内的行,然后合并第二次选择的剩余行。请注意,您不需要对第二个 select 语句设置限制:

UNION 的默认行为是从结果中删除重复的行。可选的 DISTINCT 关键字除了默认值外没有任何作用,因为它还指定重复行删除。使用可选的 ALL 关键字,不会发生重复行删除,并且结果包括所有 SELECT 语句中的所有匹配行。

You can do this using the UNION syntax:

SELECT * FROM `your_table` LIMIT 5, 5 UNION SELECT * FROM `your_table`

This will first select rows within your limit, and then combine the remainder from the second select. Note that you don't need to set a limit on the second select statement:

The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.

丿*梦醉红颜 2024-09-26 02:23:07

我不认为使用简单的 Select 可以实现这一点(我可能是错的)。我想你需要一个存储过程。

I don't think this might be achieved using a simple Select (I may be wrong). I think you'll need a stored procedure.

于我来说 2024-09-26 02:23:07

您已将其标记为 Oracle,尽管您的 SQL 语法对 Oracle 无效,因为它不支持 LIMIT

但是,这里有一个适用于 Oracle 的解决方案:

select *
  from ( select rownum as rn,
                user_id
           from admin_user
          order by user_id
       ) X
 where X.rn > :startRows
   and X.rn <= :startRows + :limitRows
 order by case when X.rn <= :baseRef
                    then X.rn + :limitRows
               else
                    X.rn
          end ASC
;

其中 :startRows 和 :limitRows 是 LIMIT 的值,并且: baseRef 是一个介于 0 和 :limitRows-1 之间的值,应该每天递增/循环(即,在第 1 天,它应该为 0;在第 2 天,1;在第 10、9 天;在第 11 天,您应该恢复为0)。您实际上可以使用当前日期,转换为 Julian 并除以 :limitRows 时取余数以自动计算 :baseRef

(根据需要替换您自己的列和表名称)

You've tagged this as Oracle, though your SQL syntax would be invalid for Oracle because it doesn't support LIMIT

However, here's a solution that will work in Oracle:

select *
  from ( select rownum as rn,
                user_id
           from admin_user
          order by user_id
       ) X
 where X.rn > :startRows
   and X.rn <= :startRows + :limitRows
 order by case when X.rn <= :baseRef
                    then X.rn + :limitRows
               else
                    X.rn
          end ASC
;

where :startRows and :limitRows are the values for your LIMIT, and :baseRef is a value between 0 and :limitRows-1 that should be incremented/cycled on a daily basis (ie on day 1 it should be 0; on day 2, 1; on day 10, 9; on day 11 you should revert to 0). You could actually use the current date, converted to Julian and take the remainder when divided by :limitRows to automate calculating :baseRef

(substitute your own column and table names as appropriate)

那片花海 2024-09-26 02:23:07

好吧,对于问题的作者来说可能有点晚了,但对人们来说可能有用。

简短回答:可以像作者要求的那样进行“旋转”。

长答案:[我将首先解释 MySQL - 我在哪里测试这个]

让我们假设我们有表 your_table (INT rn, ...)。您想要的是以特定方式排序(从 rn=N 开始“旋转”)。排序的第一个条件是 rn >= N desc。这个想法(至少我是如何理解的)是我们将顺序从 asc 更改为 desc 并将表格分成两部分(=N) 。然后我们按 rn 但 asc 顺序对其进行排序。它将独立地对每个组执行排序。所以这是我们的查询:

select * from your_table where rn between 1 and 10
order by rn >= N desc, rn asc;

如果你没有 rn 列 - 你总是可以使用带有参数的技巧,但

select t.*, @rownum := @rownum + 1 AS rn 
from your_table t,
(SELECT @rownum := 0) r
where @rownum < 10 /* here be careful - we already increased by 1 the rownum */
order by @rownum >=N - 1 desc, /* another tricky place (cause we already increased rownum) */
         @rownum asc;

我不知道最后一个是否有效。

对于 Oracle,您始终可以使用 rownum。而且我相信你也会得到同样的结果(我没有测试过!)。

希望有帮助!

Well, it might be a little bit late for the author of the question, but could be useful for people.

Short answer: It is possible to do the "spin" like author asked.

Long answer: [I'm going to explain for MySQL first - where I tested this]

Let's imagine that we have table your_table (INT rn, ...). What you want is to sort in specific way ("spin" with beginning at the rn=N). First condition of ordering is rn >= N desc. The idea (at least how I understand this) is we change the order from asc to desc and split our table in two parts (<N and >=N). Then we order this back by rn but asc order. It will execute sorting for each group independently. So here is our query:

select * from your_table where rn between 1 and 10
order by rn >= N desc, rn asc;

If you don't have rn column - you always can use the trick with parameter

select t.*, @rownum := @rownum + 1 AS rn 
from your_table t,
(SELECT @rownum := 0) r
where @rownum < 10 /* here be careful - we already increased by 1 the rownum */
order by @rownum >=N - 1 desc, /* another tricky place (cause we already increased rownum) */
         @rownum asc;

I don't know if the last one is efficient, though.

For Oracle, you always can use rownum. And I believe that you will have the same result (I didn't test it!).

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文