简体   繁体   English

MySQL列和行条目删除

[英]MySQL Column and Row entry removal

Partial View of Database Screen shot 数据库屏幕截图的局部视图

Ok so before I start let me state I am self taught and make alot of mistakes. 好吧,所以在我开始之前,我要说我是自学成才,并且犯了很多错误。 I am also not very strong in sql query writing. 我也不太擅长sql查询编写。 Ok so I want to update this table by removing multiple membergroupids. 好的,所以我想通过删除多个membergroupids更新此表。 I dont want anyone in membergroupids 2, 192, 10, 20. Ok as you can tell by the still visible commas I have worked a basic code to do one group at a time and it leaves the commas. 我不希望membergroupids 2、192、10、20中的任何人出现。好了,您仍然可以通过可见的逗号来告诉我,我已经编写了一个基本代码来一次进行一组操作,但它们却离开了逗号。 I have over 120 membergroupids I need to clear from over 1800 rows. 我需要从1800多个行中清除120多个membergroupid。 I also need to be able to put in another piece that I will worry about once I can get something done about this first. 一旦我可以先完成一些工作,我还需要放入另一个我会担心的部分。

Ok to review. 可以审查。 Need to be able to remove above mentioned membergroupids from multiple rows in this table. 需要能够从该表的多行中删除上述成员组ID。 If I need to explain it clearer please let me know as I am still trying to figure out all this SQL stuff. 如果我需要更清楚地解释它,请告诉我,因为我仍在尝试找出所有这些SQL内容。

 UPDATE user INNER JOIN userfield ON user.userid = userfield.userid SET `membergroupids` = replace(ltrim(rtrim(replace(concat(' ', replace(`membergroupids`, ',', ' '), ' '), ' 2 ', ' '))), ' ', ',') WHERE find_in_set('2', `membergroupids`) > 0 AND `field23` <= 0 AND (`membergroupids` LIKE '%' ',2,' '%' OR `membergroupids` LIKE '2,' '%' OR `membergroupids` LIKE '%' ',2' OR `membergroupids` LIKE '2'); 

That is the Code that myself and a friend came up with but it only does one membergroupids at a time. 那是我和朋友提出的Code,但一次只执行一个membergroupids。

Is membergroups a single field? 成员组是单个字段吗? And, in your table you have any number of records that may have the same membergroup and you're looking for a way to remove them all? 而且,您的表中有许多记录可能具有相同的成员组,并且您正在寻找一种删除所有记录的方法? That's how I understand it so correct me if I'm wrong. 这就是我的理解方式,如果我错了,请纠正我。

I would use something like: 我会用类似的东西:

delete from table where membergroups in (2, 192, 10, 20)

It might not be possible, but ultimately it would be better to have your membergroupids in a separate table where one row represents each membergroupid each user belongs to. 可能不可能,但是最终最好将您的membergroupids放在单独的表中,其中一行代表每个用户所属的每个membergroupid。 Then to remove them is a simple DELETE rather than a complicated SET update. 然后删除它们是简单的DELETE而不是复杂的SET更新。

The query example you guys came up with is similar to what I'd do with your particular setup, but as you can see it can get messy after awhile. 大家想出的查询示例与我对特定设置所做的查询示例相似,但是如您所见,一段时间后它会变得混乱。

On the other hand, if you can add membergroupids to a separate table setup like: 另一方面,如果可以将membergroupids添加到单独的表设置中,例如:

CREATE table user_groups(
userid  int,
membergroupid int);

Then you can remove membergroups quite simply. 然后,您可以非常简单地删除成员组。

Example 1: Remove member groups 2, 192, 10 and 20 for all users 示例1:删除所有用户的成员组2、192、10和20

delete from user_groups where membergroupid in (2, 192, 10, 20)

Example 2: Remove member groups 2, 192, 10 and 20 for users that are NOT members of groups 2 and 22. 示例2:为不是组2和22成员的用户删除成员组2、192、10和20。

delete from user_groups where membergroupid in (2, 192, 10, 20) and userid not in (select userid from user_groups where membergroupid in (2,22));

This setup, where we use a separate table instead of a field containing a set of values may seem like its less efficient, but it's not. 在这种设置下,我们使用一个单独的表而不是一个包含一组值的字段,看起来效率较低,但事实并非如此。 Plus, you have much more flexibility when it comes to adding/removing membergroups and you don't have to deal with all the commas, concats, etc. 另外,在添加/删除成员组时,您具有更大的灵活性,并且不必处理所有逗号,连字符等。

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

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