我有一个表存储某些任意设施的“活跃”用户。 每次特定用户登录和注销时都会有插入操作。 我想要做的是如下:
检测到插入操作到数据库后,检查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 IFWhat 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更多推荐
发布评论