Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.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和PHP收件箱更新_Php_Mysql_Sql Insert_Sql Delete_Inbox - Fatal编程技术网

Mysql和PHP收件箱更新

Mysql和PHP收件箱更新,php,mysql,sql-insert,sql-delete,inbox,Php,Mysql,Sql Insert,Sql Delete,Inbox,我已经创建了一个收件箱系统。登录用户可以向其他登录用户发送消息。数据库中的usermessage表有两个字段1。userid和2。messageid。以下是本表的摘录 userid | messageid 12 | 1 13 | 1 14 | 2 15 | 2 12 | 3 15 | 3 12 | 4 14 | 4 在上述情况下,当用户“12”向用户“13”发送消息时。我希望messageid“1”移动到表的底部,以便当用户“12

我已经创建了一个收件箱系统。登录用户可以向其他登录用户发送消息。数据库中的usermessage表有两个字段1。userid和2。messageid。以下是本表的摘录

userid | messageid
12     | 1
13     | 1
14     | 2
15     | 2
12     | 3
15     | 3
12     | 4
14     | 4
在上述情况下,当用户“12”向用户“13”发送消息时。我希望messageid“1”移动到表的底部,以便当用户“12”或“13”检查其邮箱时,messageid“1”需要作为最近的对话显示在收件箱的顶部。到目前为止,我已经能够通过删除messageid“1”并为每个userid插入一个新的查询来实现这一点。这是我的代码:

DELETE FROM usermessage WHERE userid = '12' and messageid = '1';
INSERT INTO usermessage SET userid = '12', messageid = '1';
DELETE FROM usermessage WHERE userid = '13' and messageid = '1';
INSERT INTO usermessage SET userid = '13', messageid = '1';

是否可以通过一个查询完成此操作。到目前为止,我无法在stackflow中找到与我的问题相关的任何明确答案。

在没有SORT语句的情况下查询行,无法保证它们将按照创建的时间顺序进行排序。从现在起,MySQL就有可能为您提供这样的排序行为

因此,有必要找到或创建一个客观排序规则。它可以是消息的时间戳,也可以是[usermessage]表中的新时间戳列


同样值得注意的是,删除和重新创建行会影响MySQL的处理成本,因为每次删除和插入时,MySQL都需要更新[usermessage]表上的索引。

OMG。你听说过规范化数据和良好的相对数据库设计吗?如果在usermessage表上使用类似于更新当前时间戳的时间戳字段,并在select上使用更合理的顺序来显示收件箱,那么这将更好地实现。12发送到13不应该只创建一个新行吗,你不应该循环使用消息id。@heximal你的关键消息有什么帮助?请更具建设性。链接,例子,任何东西。@heximal虽然我完全同意,但我希望普通表单在这张表的优先级列表中是最重要的:-谢谢。时间戳是一个明确的答案。