Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:09:49+00:00 2026-05-26T00:09:49+00:00

I am trying to update a DB table with the following structure: user_id, group_id,

  • 0

I am trying to update a DB table with the following structure:

user_id, group_id, other_data, sequence

user_id points to a user table with all of the user data, and group_id points to a record in a group info table. other_data is nothing important. sequence is a value that is used to determine in what order users are listed. For instance, if I pull everyone from group_id 1234, the sequence is used to order them by.

I am trying to run an update to change this sequence when a user is deleted from the table. Currently, I am working with a CFML script that runs a SELECT query, loops through it, and then does individual updates. What I want to do is write the whole thing in one query.

The issue that comes up is when a user is in more than one group (which is usually the case). This is what I currently have:

UPDATE
    user_groups_tbl
SET
    sequence = sequence - 1
WHERE
        group_id IN (SELECT group_id FROM user_groups_tbl WHERE user_id = #URL.user_id#)
    AND
        sequence > (SELECT sequence FROM user_groups_tbl WHERE user_id = #URL.user_id#);

I can tell that with this query, the WHERE for the sequence is not right; this will pull some “random” sequence number, not particularly the one that is tied to the record with the proper group_id.

I don’t know if I have explained this well enough, hopefully someone will be able to see what I’m trying to do here with this… If anyone can help with the query, it would be much appreciated.

This is currently being run on a MS SQL 2005 server, but will eventually be ported over to a MySQL engine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:09:50+00:00Added an answer on May 26, 2026 at 12:09 am

    tI think this will get it:

    UPDATE
        user_groups_tbl
    SET
        sequence = sequence - 1
    WHERE
            group_id IN (SELECT group_id FROM user_groups_tbl
                         WHERE user_id = #URL.user_id#)
        AND
            sequence > (SELECT tbl2.sequence FROM user_groups_tbl tbl2
                        WHERE tbl2.user_id = #URL.user_id#
                           AND tbl2.group_id = user_groups_tbl.group_id);
    

    I added another condition to the second half of your where clause to restrict what Group_id the subquery will return. This required adding a table alias.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to a create an update statement along the following lines: TABLE car:
I am trying to update table A with data from table B. I thought
I am trying to update a field in a table with data from another
I'm trying to run the following query using phpMyAdmin: UPDATE TABLE x SET `number`
I have following HTML table: <form method=post action=update-table.php> <table class=table-data style=width: 960px;> <thead> <tr>
I'm trying to update supplier table on both PostgreSQL and MySQL using the following
Im trying to do something like the following: UPDATE table SET fieldA='valueA' WHERE id='id1',
I am trying to update my table t1 which has rows as following :-
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET
I'm trying to update one table based on values in another table. What's wrong

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.