vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=251)
-   -   SQL query to delete secondary usergroup (https://vborg.vbsupport.ru/showthread.php?t=273958)

kh99 11-21-2011 12:12 PM

Quote:

Originally Posted by nerbert (Post 2270333)
Wouldn't it leave a double comma where the number was excised?

Exactly - or a starting or ending comma. But last time I checked, the vb code that reads secondary groups explode()s the string then ignores any non-numeric, null, or duplicate values, so I'm pretty sure it wouldn't matter. Still, a mod might read that field and not be able to handle it, so I suppose there's some risk.

nerbert 11-21-2011 12:16 PM

I think you would need

REPLACE(REPLACE(membergroupids, '2,', ''), ',2', '' )

First it gets rid of 2 everywhere but at the end, then catches it at the end.

kh99 11-21-2011 12:18 PM

Yeah, I think that works. But you need a third REPLACE in case the field is only "2".

Pretty cool.

nerbert 11-21-2011 12:36 PM

Code:

REPLACE(
    REPLACE(
          REPLACE(
              REPLACE(membergroupids, '2', ' '), ', ,', ''), ' ,', ''), ', ', '')

EDIT: replace 2 with a one character blank space instead of nothing. Be sure to test this where it can't do any damage to the real table

kh99 11-21-2011 01:16 PM

You know, I just thought of something (and this applies to TheLastSuperman's solution as well) - you can't just replace all 2's because someone could have a usergroup 12, or 20 something. I think you'd need a regular expression to match ^2, and ,2$ and well as ,2,.

Edit: I guess what I should say is that I think it will work for a one-time removal of some secondary groups if there's no conflict as is mentioned above. For instance if you want to remove secondary group 9 and you have less than 19 usegroups, it'll work fine.

nerbert 11-21-2011 01:27 PM

If I were doing this I would select a batch and loop through it using php to manipulate the strings and then update one at a time in the loop. It would be slow but I'd rather have slow and predictable than fast and unpredictable. You could also check what's going on before running it with the update query and be sure you aren't messing up. If the whole process takes an hour to run, so be it.

--------------- Added [DATE]1321890191[/DATE] at [TIME]1321890191[/TIME] ---------------

First I would use a SELECT That uses LIKE to match the membergroupid.

I would use this php in a loop. If everything looks OK then replace the echo with an update

PHP Code:

$m $membergroupids;
$m ',' $m ',';
$m str_replace(',2,'','$m);
$m substr($m1, -1);
echo 
$membergroupids '  --------------->  ' $m '<br>'

--------------- Added [DATE]1321914118[/DATE] at [TIME]1321914118[/TIME] ---------------

Here's the first thing I would do. Use this for a plugin in showthread_start. All it does is check that the string manipulation is working exactly as it should. You should have a white page with 10 lines each showing user name, primary usergroup, original secondary usergoup string and finally the modified secondary user group string. Edit in your userid in the first line where it says '1'

PHP Code:


if($vbulletin->userinfo['userid'] == '1'
{
    
ini_set('display_errors''1');
    
$users $db->query_read(
        SELECT usergroupid, username, membergroupids 
        FROM " 
TABLE_PREFIX "user 
        WHERE usergroupid <> '6'
        AND CONCAT(',', membergroupids, ',') LIKE CONCAT(',', usergroupid, ',')
        LIMIT 10 
        "
); 
    
    if (
$db->num_rows($users) > 0
    { 
        while(
$user $db->fetch_array($users)) 
        {
            
$n $user['username'];
            
$u $user['usergroupid'];
            
$m1 $user['membergroupids'];
            
$m2 ',' $m1 ',';
            
$find ',' $u ',';
            
$m2 str_replace($find','$m2);
            
$m2 substr($m21, -1);
            echo 
$n ' ; ' .$u ' ; ' $m1 ' -----> '$m2 '<br><br>';
        }
        die(
$db->num_rows($user) . " found");
    }
    else
    {
        die(
"no users found");
    }


If it's working perfectly I'll write a new one with an UPDATE query


All times are GMT. The time now is 10:58 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01976 seconds
  • Memory Usage 1,745KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete