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

  • Home
  • SEARCH
  • 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 8825149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:48:36+00:00 2026-06-14T06:48:36+00:00

I’ve been banging my head against the wall for days now trying to write

  • 0

I’ve been banging my head against the wall for days now trying to write a query for the following problem (new to MySQL).

I have a table A which has the following data

id, r1, r2, r3, r4, r5, r6, r7, r8, r9, max
 1,  1,  2,  3,  6,  7,  8,  9,  0
 2,  1,  3,  4,  5,  6,  7,  8,  0
 3,  2,  4,  5,  7,  9, 11, 12,  0

I need to set the max field to the maximum sequence in the row. for example, row id 1 would have a max sequence of 4 (6,7,8,9) so the max field will display 4. The numbers are always listed from smallest to largest (left to right) in the rows.

I suspect it would be something along the lines of;

SET @r:=0
SET @s:=0

IF(r1 + 1 = r2, IF(@r > @s, @s:=@r, @r:= @r + 1),
IF(r2 + 1 = r3, IF(@r > @s, @s:=@r, @r:= @r + 1),
IF(r3 + 1 = r4, IF(@r > @s, @s:=@r, @r:= @r + 1),
IF(r4 + 1 = r5, IF(@r > @s, @s:=@r, @r:= @r + 1),`

and so forth… but it doesn’t seem to work as the increment only applies per line??

FOR i <= count(column)
IF count > sequence 
THEN sequence = count
ELSE count ++
NEXT i

Any help would be greatly appreciated. Thanks.

  • 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-06-14T06:48:38+00:00Added an answer on June 14, 2026 at 6:48 am

    You probably can do this in MySQL entirely, but I think this will be rather messy.

    If I were you, I would do this on a client. This should work as Perl implementation:

    use DBI;
    my $dbh = DBI->connect("dbi:mysql", "user", "password");
    my $sth = $dbh->prepare("SELECT * FROM mytable");
    $sth->execute();
    while (my $row = $sth->fetchrow_hashref()) {
        my $id = $row->{id};
        my @r;
        push @r, $row->{r1}, $row->{r2}, $row->{r3},
                 $row->{r4}, $row->{r5}, $row->{r6},
                 $row->{r7}, $row->{r8}, $row->{r9};
        my $max = 1; # max run
        my $t = 1;   # local max run
        for (my $i = 1; $i < 9; $i++) {
            if ($r[$i] == $r[$i-1]+1) {
                $t++;
            } else {
                $t = 1;
            }
            if ($t > $max) {
                $max = $t;
            }
        }
        $dbh->do(qq{ UPDATE mytable SET max = ? WHERE id = ?}, undef,
                                       $max,         $id);
    }
    $sth->finish();
    $dbh->disconnect();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I've tracked down a weird MySQL problem to the two different ways I was
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.