如何在缺失序列(自动增量)ID中查找/插入数据?

时间:2015-09-24 05:17:15

标签: php mysql

我有一个包含

的表格
400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response.
message - Bad Authentication data.
code - 215

Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=4be80492 or
    http://www.google.co.jp/search?q=0a6306df
TwitterException{exceptionCode=[4be80492-0a6306df], statusCode=400, message=Bad Authentication data., code=215, retryAfter=-1, rateLimitStatus=null, version=4.0.4}
    at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
    at twitter4j.HttpClientBase.request(HttpClientBase.java:57)
    at twitter4j.HttpClientBase.get(HttpClientBase.java:75)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1786)
    at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:131)
    at com.twitter.ibeat.iBeatTwitter.TwitterTimeLine.main(TwitterTimeLine.java:33)
400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response.
message - Bad Authentication data.
code - 215

数据序列有3个缺失的ID值:2,3和5。

这是我的疑问:

ID(P)(auto)  NAME
----------------------- 
1            Art
4            Bankie
6            Cara

如何使用INSERT来代替缺少的ID值,以便使用所有ID。

2 个答案:

答案 0 :(得分:1)

出于性能目的,您所要求的并不是必需的,但据我所知,没有办法自动执行此操作。您必须检查缺少的ID范围并手动分配它们。

我还建议查看以下关于SQL注入保护的帖子,因为它是维护安全数据库连接的必要步骤。 SQL Injection Prevention

其次,请不要使用php的mysql_query()函数。这些已被弃用,已经有一段时间了。建议使用MySQLi或PDO。

答案 1 :(得分:0)

可以在sql查询中使用变量来完成。

让我们说$name = $_POST['fname'];

然后你需要像这样插入查询:

insert into yourtable(id,name)
select id-diff as new_id,'$name' from(
    select id,id - (@x := @x + 1) as diff
    from yourtable
    join (select @x := 0) as t1
    order by id asc
) as t2
where diff <> 0
limit 1