如果条目在数据库中,则丢弃插入操作并删除条目(if entry is in database then discard insert operation and delete entry)

编程入门 行业动态 更新时间:2024-10-12 20:21:04
如果条目在数据库中,则丢弃插入操作并删除条目(if entry is in database then discard insert operation and delete entry)

我有一个表存储某些任意设施的“活跃”用户。 每次特定用户登录和注销时都会有插入操作。 我想要做的是如下:

检测到插入操作到数据库后,检查MYSQL数据库中是否已存在用户名。 如果存在用户名,则不要插入和删除现有条目。 如果用户名不存在,则插入。

有没有办法实现这个目标? 我对MYSQL相当新,所以如果我说的话没有意义,我会事先道歉 - 只要求我澄清一下。

I have a table which stores "active" users of some arbitrary facility. There is an insert operation for every time a particular user logs on and logs off. What I want to be able to do is as follows:

Upon detection of an insert operation into the database, check if the username is already present in the MYSQL database. If the username is present, then do not insert and delete the existing entry. If the username is not present, then insert.

Is there any way to achieve this? I am fairly new to MYSQL so I apologize in advance if anything I've said doesn't make sense - just ask for my clarification.

最满意答案

我想要做的是如下:

检测到插入操作到数据库后,检查MYSQL数据库中是否已存在用户名。

是的,你可以做到这一点

如果存在用户名,则不要插入和删除现有条目。

您可以手动搜索用户名,并根据需要执行条件语句。

如果用户名不存在,则插入。

一样。

以下是您想要的示例基础:

--INPUT @Username IF EXISTS(SELECT * FROM activeUsers WHERE Username = @Username) THEN DELETE FROM activeUsers WHERE Username = @Username ELSE INSERT INTO activeUsers (columns...) VALUES (values) END IF

What I want to be able to do is as follows:

Upon detection of an insert operation into the database, check if the username is already present in the MYSQL database.

Yes, you can achieve this

If the username is present, then do not insert and delete the existing entry.

You can manually search the username, and do the condition-statement as you want.

If the username is not present, then insert.

Same thing.

Here's an example base from what you want:

--INPUT @Username IF EXISTS(SELECT * FROM activeUsers WHERE Username = @Username) THEN DELETE FROM activeUsers WHERE Username = @Username ELSE INSERT INTO activeUsers (columns...) VALUES (values) END IF

更多推荐

本文发布于:2023-08-07 18:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   数据库中   操作   entry   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!