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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:58:11+00:00 2026-05-28T00:58:11+00:00

I do 3 queries to reorder the rows of my table SELECT @i:=0; UPDATE

  • 0

I do 3 queries to reorder the rows of my table

SELECT @i:=0;
UPDATE imagedata SET imagedata.rank = 0;
UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;

which I execute from top to bottom.

Question: Is ther a shorter way to do this?


thx for all the feedback..
but i have a differnet Idear now:
Somehow I have to “connect” the “kategorieID” with the “id(Primary key)” collumn
Therefore I need to store both information into the “rank” collumn in the format like:

cxxx

c=Category Number(0 to 4)
xxx = id (1 to …n)..unique!

exampl.: output:

   rank
 +------+
 + 1001 +
 + 1002 +
 + 1003 +
 + ...  +
 + 1018 +
 + ...  +
 + 2001 +
 + 2002 +
 + 1019 +
 + 1020 +

so far I did it like:

UPDATE imagedata SET imagedata.rank = (SELECT CONCAT(kategorieID,"",LPAD(id,3,'0')) ) WHERE id=88;

once that “rank” is filled with data it gives me the abillity to swap 2 id’s.

Therefore I have to:
1) get “rank” collumn of id_1 and id_2
2) get the substring (‘xxx’) of that string exampl: (“004” .. “012”)
3) exchange substrings of id_1 “rank” and id_2 “rank”

SELECT @ix1:=SUBSTRING(rank, -3) FROM imagedata WHERE id=88;
SELECT @ix2:=SUBSTRING(rank, -3) FROM imagedata WHERE id=83;

..I know how to get it but dont know how to exchange it?

  • 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-28T00:58:12+00:00Added an answer on May 28, 2026 at 12:58 am

    I would modify the second query to only update rows that WON’T be updated in the third, like so:

    SELECT @i:=0;
    UPDATE imagedata SET imagedata.rank = 0 WHERE imagedata.kategorieID <> 1;
    UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;
    

    EDIT: Per Lamak’s comment, if the kategorieID could be NULL you’ll want to do this instead:

    SELECT @i:=0;
    UPDATE imagedata SET imagedata.rank = 0 WHERE imagedata.kategorieID <> 1 OR imagedata.kategorieID IS NULL;
    UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;
    

    Also, it’s possible that this solution may be slower than yours, especially if there isn’t an index on kategorieID. I say this because even though you are updating less rows, it now has to filter on that WHERE first. Perhaps you should test the performance of this first to see if it is truly faster.

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

Sidebar

Related Questions

Which of the two queries is more efficient? SELECT *FROM Emp WHERE DeptNo<>20 SELECT
The following two queries each give the same result: SELECT column FROM table GROUP
The queries are as simple as that: Eg 1: UPDATE threads SET hits =
UPDATE queries work fine from the mysql command line, for any user. However, when
The following queries update the same table like 7 times.. I would appreciate your
I have two queries with subqueries for dateparts which I like to join. SELECT
I have 1000 records in a table which holds position field starting from 1
I have two seperate queries: SELECT `ad_general`.`id` FROM (`ad_general`) WHERE `ad_general`.`city` = '708' ORDER
I have the following queries, QryStockOnHand SELECT QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand FROM QryStockLevel
These queries both give the result I expect: SELECT sex FROM ponies ORDER BY

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.