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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:42:44+00:00 2026-06-16T04:42:44+00:00

I have a table with an auto increment key id , item_no can be

  • 0

I have a table with an auto increment key id, item_no can be either one or two rows in a row (so they always have consecutive ids) that share the same ref but have different right/left (but technically item_no can be repeated multiple times throughout the table but that’s not an issue), and description will sometimes be the same on the consecutive rows but sometimes different:

id | item_no | description | right\left | ref
1  | 1       | a1          | right      | aaa
2  | 1       | a1          | left       | aaa
3  | 2       | b1          | right      | bbb
4  | 3       | c1          | right      | ccc
5  | 3       | c2          | left       | ccc
6  | 4       | d1          | right      | ddd
7  | 4       | d1          | left       | ddd

My issue is that I need item_no to append a -r or -l on to its value if the description of its ‘matching’ row is different.

So the result I am looking for is:

id | item_no | description | right\left | ref
1  | 1       | a1          | right      | aaa
2  | 1       | a1          | left       | aaa
3  | 2       | b1          | right      | bbb
4  | 3-r     | c1          | right      | ccc
5  | 3-l     | c2          | left       | ccc
6  | 4       | d1          | right      | ddd
7  | 4       | d1          | left       | ddd

I am exporting the table to a csv but am not using much php, just a mysql statement and then looping out the results, is this possible within the mysql statement or will I have to rely on a php loop?

  • 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-16T04:42:46+00:00Added an answer on June 16, 2026 at 4:42 am

    I would use this:

    update
      items inner join
      (select item_no from items
       group by item_no
       having count(distinct description)>1) dup
      on items.item_no=dup.item_no
    set
      items.item_no=concat(items.item_no, '-', substr(rightleft, 1,1))
    

    If rows are always consecutive, you could also use this:

    update
      items i1 inner join items i2
      on (i1.id=i2.id+1 or i1.id=i2.id-1) 
         and (i1.item_no=i2.item_no)
         and (i1.description<>i2.description)
    set i1.item_no=concat(i1.item_no, '-', substr(i1.rightleft, 1,1))
    

    EDIT: if rows are always consecutive, and you just need a select and not an update, you could use this:

    select
      i1.id,
      case when i1.description=i2.description or i2.id is null then i1.item_no else
                concat(i1.item_no, '-', substr(i1.rightleft, 1,1)) end,
      i1.description, i1.rightleft, i1.ref
    from
      items i1 left join items i2
      on (i1.id=i2.id+1 or i1.id=i2.id-1) and (i1.item_no=i2.item_no)
    order by i1.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a primary key that is auto increment. I want
I have a MySQL table with an auto increment primary key. I deleted some
I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table with a auto-incremented primary key: user_id. For a currently theoretical
I have a mysql table with a auto incremented key. I need a way
I have a MySQL table with an auto-incremented integer primary key. I want to
I have table with an auto-increment field, but I need to transfer the table
In MySql I could have a table with an auto increment column and insert

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.