简体   繁体   English

MySQL限制

[英]MySQL order by limit

I am trying to understand the following snippet of a complicated MYSQL procedure, where var1, var2, var3 and var4 are declared variables: 我试图理解以下复杂的MYSQL过程的代码段,其中var1,var2,var3和var4被声明为变量:

 select col1, col2, col3, col4 from table order by col1 limit 5, 1 into var1, var2, var3, var4; 

In particular, I don't know what the above is inserting into var1, var2, var3 and var4. 特别是,我不知道上面将什么插入到var1,var2,var3和var4中。

More generally, I don't understand what the following does in MYSQL: 更普遍地说,我不了解MYSQL中的以下功能:

 select...limit ..., 1 into 

Any help would be great! 任何帮助将是巨大的!

Lets dissect the query. 让我们剖析查询。

select col1, col2, col3, col4 from table;

This gives you all the rows from the table, but only with 4 columns. 这将为您提供表中的所有行,但只有4列。

select col1, col2, col3, col4 from table order by col1;

Now you are are ordering the results in the ascending order of col1. 现在,您要按col1的升序对结果进行排序。

select col1, col2, col3, col4 from table order by col1 limit 5, 1;

Here 5 is the offset and 1 is the actual limit , which will reduce your result set to a size of 1. 这里5offset1是实际limit ,这会将您的结果集减小为1。

What does offset mean? offset是什么意思?

Suppose you get 10 results from the second query. 假设您从第二个查询中获得10个结果。 Then this third query will give you results starting from the 6th row and limited by the limit passed, which in this case is 1. Hence, you will only get one row having four values. 然后,第三个查询将为您提供从第6行开始的结果,并受所通过的限制(在本例中为1)进行限制。因此,您只会得到一行包含四个值的结果。

What does into do? 什么into做?

It simply stores the values of those 4 results into those 4 variables. 它只是将这4个结果的值存储到这4个变量中。

So LIMIT can take two paramaters LIMIT offset, count 因此LIMIT可以取两个参数LIMIT偏移量,即

offset is the offset of the first row you want to return (for example offset of row 1 would be 0) offset是要返回的第一行的偏移量(例如,第1行的偏移量将为0)

count is the max amount of rows that you want to return count是要返回的最大行数

If you pass it just one parameter then that's just a count so LIMIT 5 would return the first 5 rows in the table. 如果仅传递一个参数,则仅是一个计数,因此LIMIT 5将返回表中的前5行。

so your query is returning the 4 values of col1, col2, col3 and col4 that are in row 6 and then is returning those values into var1, var2, var3, var4 因此您的查询返回第6行的col1,col2,col3和col4的4个值,然后将这些值返回到var1,var2,var3,var4

(I think... I haven't used anything like that in a while... so I stand to be corrected) (我认为...我已经有一段时间没有使用过类似的东西了,所以我会得到纠正)

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

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