简体   繁体   English

在MySQL中删除第一行并将元素添加到最后一行

[英]Deleting first row and adding element to last row iny MySQL

I have a table called message_pool and it only has one attribute called message . 我有一个名为message_pool的表,它只有一个称为message属性。 For example: 例如:

hello 1
hello 2
hello 3

I want to delete first row and add myValue to last row . 我想删除第一行 并将 myValue 添加最后一行 I have basically two queries. 我基本上有两个查询。

$query_one = "DELETE FROM message_pool LIMIT 1";
$query_two = "INSERT INTO message_pool VALUES ('$myValue');"

If I only run $query_one this result happens: 如果我只运行$query_one发生以下结果:

hello 2
hello 3

If I only run $query_two this result happens: 如果我只运行$query_two发生以下结果:

hello 1
hello 2
hello 3
myValue

ODLY! 好! If I first run $query_one then $query_two this result happens: 如果我第一次运行$query_one然后$query_two这种结果发生的:

myValue
hello 2
hello 3

But I want this result to get. 但是我希望得到这个结果。

hello 2
hello 3
myValue

How come it places myValue to the first row, but when I run the second query standalone it places myValue to last row? 它如何将myValue放在第一行,但是当我独立运行第二个查询时,它将myValue最后一行?

Just add a primary key to your table and set it to autoincrement. 只需在表中添加一个主键并将其设置为自动增量即可。 It is a good practice to have primary key in every table. 在每个表中都有主键是一个好习惯。

ALTER TABLE `message_pool` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`);

Your above query work fine after you execute above code. 执行以上代码后,上述查询工作正常。

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

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