减少自动增量列?

时间:2013-02-10 05:16:36

标签: php mysql

好吧所以我有这个脚本可以更新或插入值到mysql中,也可以自动增加一列,并将枚举值从0设置为1.

现在我试图弄清楚我如何反转它,以便将枚举值设置回0(我想我知道该怎么做)我也需要弄清楚我如何减少或者反之亦然增量是,在自动增量列中是否可以为-1?

只要我可以递减自动增量列并将枚举值从1变为0,就不一定需要删除这些值。

有谁能告诉我,请问我该如何做到这一点?感谢。

<?php

    require_once('includes/session.php');
    require_once('includes/functions.php');
    require('includes/_config/connection.php');

    session_start();

        confirm_logged_in();

        if (isset ($_GET['to'])) {
        $user_to_id = $_GET['to'];


    }


    if (!isset($_GET['to']))
        exit('No user specified.');

    $user_id = $_GET['to'];


    $result = mysql_query("SELECT * FROM ptb_likes WHERE liked_id ='".$user_to_id."' ");

    if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE ptb_likes SET likes = likes +1 WHERE liked_id = '".$user_to_id."' ");


        $user_to_id = mysql_query("ALTER TABLE likes AUTO_INCREMENT = $id");
    }
    else
    {
        mysql_query("INSERT INTO ptb_likes (user_id, liked_id) VALUES (".$_SESSION['user_id'].", ".$user_to_id.")"); 

    }



    if($result) 
    { 
    mysql_query("UPDATE ptb_likes SET user_id_has_liked='1' WHERE user_id=".$_SESSION['user_id']."") 
    or die(mysql_error());

    header("Location: {$_SERVER['HTTP_REFERER']}");

    }
    ?>

1 个答案:

答案 0 :(得分:0)

正如其他地方所建议的

create table b
(
id int not null auto_increment primary key,
foo boolean not null
);

 insert into b (foo) values (true), (false), (false),  (true), (true);

update b set
foo = not foo;