MySql-如何按设置的时间间隔选择ID的范围 [英] MySql - How to select a range of id's at set intervals

查看:153
本文介绍了MySql-如何按设置的时间间隔选择ID的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况,我在表类别中有一个名为categoryid的表列,我需要选择表中categoryid为100或更高且间隔为100的所有行.

I have the following scenario, I have a table column with the name categoryid in table categories, I need to select all rows within the table where categoryid is 100 and above in intervals of 100.

例如

SELECT * FROM categories WHERE categoryid IN(100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000...

是否有比手动键入所有间隔更好的方法,它应该从100开始并增加到最后一个数字.请记住,随着客户添加新类别,最后一个数字可能会更改.这有可能吗,希望我有道理.

Is there a better way to do this than manually typing all intervals, it should start at hundred and go up to the last number. Keep in mind that the last number can change as client adds new categories. is this possible, hope I make sense.

推荐答案

因此,我使用的这个%运算符称为取模运算符,它将除以一个数(在本例中为100)后会得到余数.如果余数为0,则为100的倍数.我们还需要> = 100条件,因为零也将传递模数条件.

So this % operator that I have used is called the modulo operator and it will give you the remainder after dividing by a number (in this case 100). If the remainder is 0 then it's a multiple of 100. We also need the >= 100 condition because zero also passes the modulo condition.

SELECT * FROM categories WHERE categoryid >= 100 AND categoryid % 100 = 0;

这篇关于MySql-如何按设置的时间间隔选择ID的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆