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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:09+00:00 2026-05-27T04:09:09+00:00

Ok, I have a SQL query that I’m trying to generate that will combine

  • 0

Ok, I have a SQL query that I’m trying to generate that will combine entries based on some logic. I eventually need this to cascade, but I’m running into some issues with things being combined more than I want them to.

Let’s see if I can illustrate. I have a table:


CREATE TABLE IF NOT EXISTS doc_lines (
id bigint(20) NOT NULL AUTO_INCREMENT,
file_id bigint(20) NOT NULL,
line int(4) NOT NULL,
end_line int(4) NOT NULL,
type VARCHAR(100),
text VARCHAR( 5120 ) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY file_id (file_id,line);

                          INSERT INTO doc_lines VALUES(1,1,1,1,NULL, 'abcdefg');
                          INSERT INTO doc_lines VALUES(1,1,2,2,NULL, 'hijkl');
                          INSERT INTO doc_lines VALUES(1,1,3,3,NULL, 'mn');
                          INSERT INTO doc_lines VALUES(1,1,4,4,NULL, 'op');
                          INSERT INTO doc_lines VALUES(1,1,5,5,NULL, 'qrs');
                          INSERT INTO doc_lines VALUES(1,1,6,6,NULL, 'tuv.');
                          INSERT INTO doc_lines VALUES(1,1,7,7,NULL, 'wxy');
                          INSERT INTO doc_lines VALUES(1,1,8,8,NULL, 'zab');

I’m trying to combine the values of “text” when two lines in a row match a certain condition.

e.g. My existing query is something like the following:


UPDATE doc_lines AS a
JOIN doc_lines AS b ON a.file_id = b.file_id AND a.end_line + 1 = b.line
SET a.end_line = b.end_line, b.type=”DELETE”, a.text=CONCAT(a.text, ” “, TRIM(b.text))
WHERE b.text REGEXP ‘^[a-z]$’;

I then follow it up with a:


DELETE from doc_lines WHERE ‘type’=”DELETE”;

The problem I’m having is that line 1 matches line 2, which flags line 2 for delete….
Line 2 matches line 3, which flags line 3 for delete…
Line 3 matches line 4, which flags line 4, for delete…
etc

As a result I end up deleting more lines than I want.

At first I thought I could do this to make it skip every other line:


UPDATE doc_lines AS a
JOIN doc_lines AS b ON a.file_id = b.file_id AND a.end_line + 1 = b.line
SET a.end_line = b.end_line, b.type=”DELETE”, a.text=CONCAT(a.text, ” “, TRIM(b.text))
WHERE b.text REGEXP ‘^[a-z]$’ AND a.type <> “DELETE”;

But the update of one entry in the query doesn’t seem to take effect until after the query is done, as a result above doesn’t behave any differently…

As a result I thought, “Well, why not handle all the odd lines, then all the even?”, so I updated my query appropriately:


UPDATE doc_lines AS a
JOIN doc_lines AS b ON a.file_id = b.file_id AND a.end_line + 1 = b.line
SET a.end_line = b.end_line, b.type=”DELETE”, a.text=CONCAT(a.text, ” “, TRIM(b.text))
WHERE b.text REGEXP ‘^[a-z]$’ AND a.line % 2=0;

The problem with this that I need to run the query more than once, because eventually I want lines 1-6 combined and 7-8 (using my example). Each subsequent call combines the lines with the line after it, when it matches.

The problem with this is that eventually I end up hitting the same situation as with my original query and I’m flagging some line for deleting that was also used to flag other lines for deletion.

Even if I end up rotating odd and even on the lines, or the id, or the end_line, at some point there appears to end up being an overlap.

Any ideas? Is there a way to process every-other entry in a database, not based on its actual value?

  • 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-27T04:09:10+00:00Added an answer on May 27, 2026 at 4:09 am

    ok, I figured something out that works.

    If anyone has anything better, or more efficient… please let me know.

    My solution was to create a temporary table which I join to table a:

    CREATE TABLE update_index SELECT id, @row := @row + 1 as row FROM doc_lines, (SELECT @row := 0) AS r;
    

    I can then join to that table, alternate even then delete, then odd then delete, then recreate the temporary table and repeat.

    Its a lot of steps, but it works.

    I’d definitely be interested in a more elegant or efficient method!

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

Sidebar

Related Questions

I have a SQL query that I'm trying to debug. It works fine for
I currently have a SQL query that returns a number of fields. I need
I have an Sql query that looks up a person based on SSN and
I currently have a SQL query that returns results based on a dynamic number
I have following SQL query that selects some results from my table: select avg(c3),
I have a SQL query that will result in one string. Rather than bind
I have a SQL query that will return over 10,000 rows. Since the client
I have a working sql query that I have been trying to convert to
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not

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.