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 9313467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:53:23+00:00 2026-06-19T01:53:23+00:00

I have this table in mysql: name ID B1 1 B2 1 B3 1

  • 0

I have this table in mysql:

name         ID
B1            1
B2            1
B3            1
B4            2
B5            2
B6            2
A1            3
A2            3
A3            3

I’d like to add data into a column, notifying that the id will change at the next line. Something like this:

name       beforeChange     ID
 B1          NO               1
 B2          NO               1
 B3          YES              1
 B4          NO               2
 B5          NO               2
 B6          YES              2
 A1          NO               3
 A2          NO               3
 A3          NO               3
 A4          NO               3

Is there any way to do this in sql?
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-19T01:53:25+00:00Added an answer on June 19, 2026 at 1:53 am

    Ugly as sin & probably far from ideal from an efficiency point of view but seems to work:

    create table myTable (id int(10) unsigned not null,name varchar(10));
    
    insert into myTable (id,name) values (1,'B1');
    insert into myTable (id,name) values (1,'B2');
    insert into myTable (id,name) values (1,'B3');
    insert into myTable (id,name) values (2,'B4');
    insert into myTable (id,name) values (2,'B5');
    insert into myTable (id,name) values (2,'B6');
    insert into myTable (id,name) values (3,'A1');
    insert into myTable (id,name) values (3,'A2');
    insert into myTable (id,name) values (3,'A3');
    insert into myTable (id,name) values (3,'A4');
    
    select a.id,
    case 
    when b.id is null then 'NO' 
    when b.id = (select max(id) from myTable) then 'NO' -- < handle last line of results set
    else 'YES' end as beforeChange,a.name
    from
    -- rank within id
    (
    select mt.id,mt.name,
    case mt.id when @curId then @curRow := @curRow + 1 
              else @curRow := 1 and @curId := mt.id END as rank
    from myTable mt
    join (select @curRow := 0, @curId := -1) r
    order by id,name asc
    ) a
    left outer join
    -- max rank by id
    (
    select t.id,max(t.rank) as maxRank
    from (
    select mt.*,
    case mt.id
            when @curId 
            then @curRow := @curRow + 1 
            else @curRow := 1 and @curId := mt.id END
          as rank
    from myTable mt
    join (select @curRow := 0, @curId := -1) r
    order by id,name asc
    ) t 
    group by t.id
    ) b on a.id = b.id and b.maxRank = a.rank;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
Let's say I have a MySQL table that is something like this: software table:
I have a mysql table with data like (name, address, sex, city state, zip,
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have this mysql table called comments which looks like this: commentID parentID type
I am trying to write a function that will insert data into a MySQL
I have this table for users that stores their usernames and other data, thats
I need to have MySQL query like this one: UPDATE table_name SET 1 =

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.