Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 无法通过查询同一个表来更新表_Mysql - Fatal编程技术网

Mysql 无法通过查询同一个表来更新表

Mysql 无法通过查询同一个表来更新表,mysql,Mysql,我有一个表notes,描述如下: +---------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+-------------------+----------------+ |

我有一个表
notes
,描述如下:

+---------+--------------+------+-----+-------------------+----------------+
| Field   | Type         | Null | Key | Default           | Extra          |
+---------+--------------+------+-----+-------------------+----------------+
| id      | int(11)      | NO   | PRI | NULL              | auto_increment |
| date    | datetime     | YES  |     | CURRENT_TIMESTAMP |                |
| content | varchar(400) | YES  |     |                   |                |
| page    | int(11)      | YES  |     | -1                |                |
| user_id | int(11)      | YES  | MUL | NULL              |                |
| hasmore | bit(1)       | YES  |     | b'0'              |                |
+---------+--------------+------+-----+-------------------+----------------+
其中页面表示页码。
我无法创建一个触发器,该触发器可以在插入页面时自动设置笔记的页码。
原因是这个声明不起作用

update notes set page = (select count(*) from notes);
似乎我无法查询
notes
来更新
notes
本身的值。
有没有其他方法可以轻松完成我的任务

我得到的错误是:

ERROR 1093 (HY000): You can't specify target table 'notes' for update in FROM clause
编辑:
假设我的表中有两个条目

>select id, user_id, page, content from notes where user_id = 2;
1  |  2  |  0  | "hi"
2  |  2  |  1  | "welcome"
我插入新数据

>insert into notes (user_id, content) values (2, "hello");
那么我的桌子应该是这样的:

>select id, user_id, page from notes where user_id = 2;
1  |  2  |  0  |  "hi"
2  |  2  |  1  |  "welcome"
3  |  2  |  2  |  "hello"

有很多方法可以做到这一点,但在此之前,请澄清update语句应该做什么,如果可能的话,请提供一些示例数据和预期的输出。我不相信您应该存储这些数据。我不擅长mysql,但我想知道不存储页码的原因。事实上,我不想从服务器端脚本完成这项工作。但是,如果从服务器端脚本进行操作是好的,那么我会这样做。一般来说,您不应该存储可以轻松从其他数据派生的数据。如果有人更新或删除了一个id怎么办?然后你所有的页码都会错,你甚至不知道。另外,您的“编辑”不能正确地代表问题。@您能建议我其他方法来做同样的事情吗?我将更改我的表,因为它不正确,但我仍然想知道如何在类似的情况下创建页码(可能使用触发器)。我不能通过为delete创建触发器来维护它。