开发者

Filtering SQL results

I would like to show a filtered result to a few ip's that keep scraping my content. I have blocked them with .htaccess and they change their ip address and continue doing it. So I thought, I want to create a soft block that won't show them all of my content and hopefully they won't even notice.

My table has a auto_increment field

 id | category |  everything else
 1       1
 2       1
 3       4
 4       2

I have been trying something like this.

SELECT * from mytable WHERE `category` = '1' having avg(id/3) = 1 ORDER BY `id` DESC LIMIT 0 , 10

I have searched forever but I am a newb to sql, s开发者_开发知识库o I don't even really know what I am searching for. I hope somebody here can please help me! Thanks :)


If you want to get remainder of division by 3, you should use % operator.

SELECT * from mytable WHERE `category` = '1' and id % 3 = 1 ORDER BY `id` 
DESC LIMIT 0 , 10


Generally, the ID column is not for doing computations on it. It does not represent anything other than unique identifier of the record (at most, it should be used to sort the records chronologically) - you could have there GUIDs for example, and your application should work.

If you want to store the IPs that you want to block in your DB, consider adding another column to your table, call it status or something similar, and store in this column the status for that ip - this status could be clean, suspicious, blocked, etc. After that, your SELECT should look only after the rows with blocked status

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜