How can you update a table with an ascending order position based on different groups in MYSQL?











up vote
1
down vote

favorite












I am struggling with this complex query. I am trying to insert the order position of some products.
For example,
I have currently table 1 with a position of NULL, I want to group each Product ID and assign each size a menu position based on ProductID group and using this FIND_IN_SET:



FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;



In other words, I want it to look like Table2.



Table1



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | NULL
2 | 100 | M | NULL
3 | 100 | L | NULL
4 | 101 | 40 | NULL
5 | 101 | 41 | NULL
6 | 101 | 42 | NULL
7 | 102 | XS | NULL
8 | 102 | L | NULL


Table2



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | 1
2 | 100 | M | 2
3 | 100 | L | 3
4 | 101 | 40 | 1
5 | 101 | 41 | 2
6 | 101 | 42 | 3
7 | 102 | XS | 1
8 | 102 | L | 2


What I collected so far:



Number of products Group:select count(distinct ProductID) from Table1



Sort size based on specific order: SELECT * FROM Table1 ORDER BY FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;










share|improve this question
























  • what's the logic behind assigning such menu position?
    – vivek_23
    Nov 11 at 18:54










  • Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
    – rd_nielsen
    Nov 11 at 19:00










  • You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
    – rd_nielsen
    Nov 11 at 19:01










  • @rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
    – Beck
    Nov 11 at 19:06










  • @vivek_23 It is for a dropdown menu
    – Beck
    Nov 11 at 19:06















up vote
1
down vote

favorite












I am struggling with this complex query. I am trying to insert the order position of some products.
For example,
I have currently table 1 with a position of NULL, I want to group each Product ID and assign each size a menu position based on ProductID group and using this FIND_IN_SET:



FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;



In other words, I want it to look like Table2.



Table1



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | NULL
2 | 100 | M | NULL
3 | 100 | L | NULL
4 | 101 | 40 | NULL
5 | 101 | 41 | NULL
6 | 101 | 42 | NULL
7 | 102 | XS | NULL
8 | 102 | L | NULL


Table2



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | 1
2 | 100 | M | 2
3 | 100 | L | 3
4 | 101 | 40 | 1
5 | 101 | 41 | 2
6 | 101 | 42 | 3
7 | 102 | XS | 1
8 | 102 | L | 2


What I collected so far:



Number of products Group:select count(distinct ProductID) from Table1



Sort size based on specific order: SELECT * FROM Table1 ORDER BY FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;










share|improve this question
























  • what's the logic behind assigning such menu position?
    – vivek_23
    Nov 11 at 18:54










  • Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
    – rd_nielsen
    Nov 11 at 19:00










  • You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
    – rd_nielsen
    Nov 11 at 19:01










  • @rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
    – Beck
    Nov 11 at 19:06










  • @vivek_23 It is for a dropdown menu
    – Beck
    Nov 11 at 19:06













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am struggling with this complex query. I am trying to insert the order position of some products.
For example,
I have currently table 1 with a position of NULL, I want to group each Product ID and assign each size a menu position based on ProductID group and using this FIND_IN_SET:



FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;



In other words, I want it to look like Table2.



Table1



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | NULL
2 | 100 | M | NULL
3 | 100 | L | NULL
4 | 101 | 40 | NULL
5 | 101 | 41 | NULL
6 | 101 | 42 | NULL
7 | 102 | XS | NULL
8 | 102 | L | NULL


Table2



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | 1
2 | 100 | M | 2
3 | 100 | L | 3
4 | 101 | 40 | 1
5 | 101 | 41 | 2
6 | 101 | 42 | 3
7 | 102 | XS | 1
8 | 102 | L | 2


What I collected so far:



Number of products Group:select count(distinct ProductID) from Table1



Sort size based on specific order: SELECT * FROM Table1 ORDER BY FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;










share|improve this question















I am struggling with this complex query. I am trying to insert the order position of some products.
For example,
I have currently table 1 with a position of NULL, I want to group each Product ID and assign each size a menu position based on ProductID group and using this FIND_IN_SET:



FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;



In other words, I want it to look like Table2.



Table1



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | NULL
2 | 100 | M | NULL
3 | 100 | L | NULL
4 | 101 | 40 | NULL
5 | 101 | 41 | NULL
6 | 101 | 42 | NULL
7 | 102 | XS | NULL
8 | 102 | L | NULL


Table2



ID   |   ProductID   |   Size   |   Menu_position
1 | 100 | S | 1
2 | 100 | M | 2
3 | 100 | L | 3
4 | 101 | 40 | 1
5 | 101 | 41 | 2
6 | 101 | 42 | 3
7 | 102 | XS | 1
8 | 102 | L | 2


What I collected so far:



Number of products Group:select count(distinct ProductID) from Table1



Sort size based on specific order: SELECT * FROM Table1 ORDER BY FIND_IN_SET(size,"UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") asc;







mysql sql if-statement while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:21

























asked Nov 11 at 18:51









Beck

175




175












  • what's the logic behind assigning such menu position?
    – vivek_23
    Nov 11 at 18:54










  • Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
    – rd_nielsen
    Nov 11 at 19:00










  • You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
    – rd_nielsen
    Nov 11 at 19:01










  • @rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
    – Beck
    Nov 11 at 19:06










  • @vivek_23 It is for a dropdown menu
    – Beck
    Nov 11 at 19:06


















  • what's the logic behind assigning such menu position?
    – vivek_23
    Nov 11 at 18:54










  • Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
    – rd_nielsen
    Nov 11 at 19:00










  • You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
    – rd_nielsen
    Nov 11 at 19:01










  • @rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
    – Beck
    Nov 11 at 19:06










  • @vivek_23 It is for a dropdown menu
    – Beck
    Nov 11 at 19:06
















what's the logic behind assigning such menu position?
– vivek_23
Nov 11 at 18:54




what's the logic behind assigning such menu position?
– vivek_23
Nov 11 at 18:54












Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
– rd_nielsen
Nov 11 at 19:00




Do the menu positions for a Product ID have to be ascending? For example, if a product had only sizes "S" and "L", should the menu positions be 1 and 2, or are positions of 1 and 3 OK?
– rd_nielsen
Nov 11 at 19:00












You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
– rd_nielsen
Nov 11 at 19:01




You can put your sizes and menu position values (integers) into a table and join to that table to get the menu position for each size.
– rd_nielsen
Nov 11 at 19:01












@rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
– Beck
Nov 11 at 19:06




@rd_nielsen Yes menu has to be chronological order 1 and 2, it cannot skip. This is why I just can't hard assign each value.
– Beck
Nov 11 at 19:06












@vivek_23 It is for a dropdown menu
– Beck
Nov 11 at 19:06




@vivek_23 It is for a dropdown menu
– Beck
Nov 11 at 19:06












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can use variables in pre-MySQL 8.0:



SELECT t1.*,
(@rn := if(@p = productid, @rn + 1,
if(@p := productid, 1, 1)
)
) as menu_position
FROM (SELECT t1.*
FROM Table1 t1
ORDER BY ProductId,
FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60') asc
) AS alias CROSS JOIN
(SELECT @p := -1, @rn := 0) params;


In MySQL 8+, this is much simpler:



select t1.*,
row_number() over (partition by productid order by FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60')) as menu_position
from table1 t1





share|improve this answer























  • You guys are beasts! Thank you so much, hope to get at your level someday!
    – Beck
    Nov 11 at 19:44










  • Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
    – Beck
    Nov 11 at 20:18












  • @Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
    – Gordon Linoff
    Nov 12 at 2:12










  • Thank you, I manage to put it into a store produce and made a temporary table.
    – Beck
    Nov 12 at 18:58


















up vote
0
down vote













Create a two-column table containing all the the Size values in one column and the integer order of those sizes in the second column--call that table menu_pos. Join this to your Table on size, to produce a table or view (call this product_pos) containing columns product_id, size, and menu_pos. Then modify the menu_pos values to ensure that they are strictly sequential using a window function, such as:



select
product_id,
size,
rank() over (partition by product_id order by menu_pos) as new_menu_pos
from
product_pos;


Window functions require MySQL 8.






share|improve this answer























  • This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
    – vivek_23
    Nov 11 at 19:35












  • The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
    – rd_nielsen
    Nov 11 at 19:37










  • @rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
    – vivek_23
    Nov 11 at 19:48










  • @vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
    – rd_nielsen
    Nov 11 at 19:53






  • 1




    @vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
    – rd_nielsen
    Nov 11 at 20:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252030%2fhow-can-you-update-a-table-with-an-ascending-order-position-based-on-different-g%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can use variables in pre-MySQL 8.0:



SELECT t1.*,
(@rn := if(@p = productid, @rn + 1,
if(@p := productid, 1, 1)
)
) as menu_position
FROM (SELECT t1.*
FROM Table1 t1
ORDER BY ProductId,
FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60') asc
) AS alias CROSS JOIN
(SELECT @p := -1, @rn := 0) params;


In MySQL 8+, this is much simpler:



select t1.*,
row_number() over (partition by productid order by FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60')) as menu_position
from table1 t1





share|improve this answer























  • You guys are beasts! Thank you so much, hope to get at your level someday!
    – Beck
    Nov 11 at 19:44










  • Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
    – Beck
    Nov 11 at 20:18












  • @Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
    – Gordon Linoff
    Nov 12 at 2:12










  • Thank you, I manage to put it into a store produce and made a temporary table.
    – Beck
    Nov 12 at 18:58















up vote
1
down vote



accepted










You can use variables in pre-MySQL 8.0:



SELECT t1.*,
(@rn := if(@p = productid, @rn + 1,
if(@p := productid, 1, 1)
)
) as menu_position
FROM (SELECT t1.*
FROM Table1 t1
ORDER BY ProductId,
FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60') asc
) AS alias CROSS JOIN
(SELECT @p := -1, @rn := 0) params;


In MySQL 8+, this is much simpler:



select t1.*,
row_number() over (partition by productid order by FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60')) as menu_position
from table1 t1





share|improve this answer























  • You guys are beasts! Thank you so much, hope to get at your level someday!
    – Beck
    Nov 11 at 19:44










  • Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
    – Beck
    Nov 11 at 20:18












  • @Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
    – Gordon Linoff
    Nov 12 at 2:12










  • Thank you, I manage to put it into a store produce and made a temporary table.
    – Beck
    Nov 12 at 18:58













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can use variables in pre-MySQL 8.0:



SELECT t1.*,
(@rn := if(@p = productid, @rn + 1,
if(@p := productid, 1, 1)
)
) as menu_position
FROM (SELECT t1.*
FROM Table1 t1
ORDER BY ProductId,
FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60') asc
) AS alias CROSS JOIN
(SELECT @p := -1, @rn := 0) params;


In MySQL 8+, this is much simpler:



select t1.*,
row_number() over (partition by productid order by FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60')) as menu_position
from table1 t1





share|improve this answer














You can use variables in pre-MySQL 8.0:



SELECT t1.*,
(@rn := if(@p = productid, @rn + 1,
if(@p := productid, 1, 1)
)
) as menu_position
FROM (SELECT t1.*
FROM Table1 t1
ORDER BY ProductId,
FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60') asc
) AS alias CROSS JOIN
(SELECT @p := -1, @rn := 0) params;


In MySQL 8+, this is much simpler:



select t1.*,
row_number() over (partition by productid order by FIND_IN_SET(size, 'UNI,XS,S,M,L,XL,XXL,3XL,4XL,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60')) as menu_position
from table1 t1






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 19:30









Raymond Nijland

7,67121327




7,67121327










answered Nov 11 at 19:25









Gordon Linoff

751k34286394




751k34286394












  • You guys are beasts! Thank you so much, hope to get at your level someday!
    – Beck
    Nov 11 at 19:44










  • Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
    – Beck
    Nov 11 at 20:18












  • @Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
    – Gordon Linoff
    Nov 12 at 2:12










  • Thank you, I manage to put it into a store produce and made a temporary table.
    – Beck
    Nov 12 at 18:58


















  • You guys are beasts! Thank you so much, hope to get at your level someday!
    – Beck
    Nov 11 at 19:44










  • Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
    – Beck
    Nov 11 at 20:18












  • @Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
    – Gordon Linoff
    Nov 12 at 2:12










  • Thank you, I manage to put it into a store produce and made a temporary table.
    – Beck
    Nov 12 at 18:58
















You guys are beasts! Thank you so much, hope to get at your level someday!
– Beck
Nov 11 at 19:44




You guys are beasts! Thank you so much, hope to get at your level someday!
– Beck
Nov 11 at 19:44












Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
– Beck
Nov 11 at 20:18






Is there a way I can add this as a view? I keep getting Error 1351: View's SELECT contains a variable or parameter SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED
– Beck
Nov 11 at 20:18














@Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
– Gordon Linoff
Nov 12 at 2:12




@Beck . . . There is not an easy way to add this to a view. It is possible, but not really worth the effort, because the view would have to use a correlated subquery and would be expensive.
– Gordon Linoff
Nov 12 at 2:12












Thank you, I manage to put it into a store produce and made a temporary table.
– Beck
Nov 12 at 18:58




Thank you, I manage to put it into a store produce and made a temporary table.
– Beck
Nov 12 at 18:58












up vote
0
down vote













Create a two-column table containing all the the Size values in one column and the integer order of those sizes in the second column--call that table menu_pos. Join this to your Table on size, to produce a table or view (call this product_pos) containing columns product_id, size, and menu_pos. Then modify the menu_pos values to ensure that they are strictly sequential using a window function, such as:



select
product_id,
size,
rank() over (partition by product_id order by menu_pos) as new_menu_pos
from
product_pos;


Window functions require MySQL 8.






share|improve this answer























  • This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
    – vivek_23
    Nov 11 at 19:35












  • The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
    – rd_nielsen
    Nov 11 at 19:37










  • @rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
    – vivek_23
    Nov 11 at 19:48










  • @vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
    – rd_nielsen
    Nov 11 at 19:53






  • 1




    @vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
    – rd_nielsen
    Nov 11 at 20:03















up vote
0
down vote













Create a two-column table containing all the the Size values in one column and the integer order of those sizes in the second column--call that table menu_pos. Join this to your Table on size, to produce a table or view (call this product_pos) containing columns product_id, size, and menu_pos. Then modify the menu_pos values to ensure that they are strictly sequential using a window function, such as:



select
product_id,
size,
rank() over (partition by product_id order by menu_pos) as new_menu_pos
from
product_pos;


Window functions require MySQL 8.






share|improve this answer























  • This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
    – vivek_23
    Nov 11 at 19:35












  • The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
    – rd_nielsen
    Nov 11 at 19:37










  • @rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
    – vivek_23
    Nov 11 at 19:48










  • @vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
    – rd_nielsen
    Nov 11 at 19:53






  • 1




    @vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
    – rd_nielsen
    Nov 11 at 20:03













up vote
0
down vote










up vote
0
down vote









Create a two-column table containing all the the Size values in one column and the integer order of those sizes in the second column--call that table menu_pos. Join this to your Table on size, to produce a table or view (call this product_pos) containing columns product_id, size, and menu_pos. Then modify the menu_pos values to ensure that they are strictly sequential using a window function, such as:



select
product_id,
size,
rank() over (partition by product_id order by menu_pos) as new_menu_pos
from
product_pos;


Window functions require MySQL 8.






share|improve this answer














Create a two-column table containing all the the Size values in one column and the integer order of those sizes in the second column--call that table menu_pos. Join this to your Table on size, to produce a table or view (call this product_pos) containing columns product_id, size, and menu_pos. Then modify the menu_pos values to ensure that they are strictly sequential using a window function, such as:



select
product_id,
size,
rank() over (partition by product_id order by menu_pos) as new_menu_pos
from
product_pos;


Window functions require MySQL 8.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 19:32

























answered Nov 11 at 19:25









rd_nielsen

1,6152615




1,6152615












  • This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
    – vivek_23
    Nov 11 at 19:35












  • The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
    – rd_nielsen
    Nov 11 at 19:37










  • @rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
    – vivek_23
    Nov 11 at 19:48










  • @vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
    – rd_nielsen
    Nov 11 at 19:53






  • 1




    @vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
    – rd_nielsen
    Nov 11 at 20:03


















  • This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
    – vivek_23
    Nov 11 at 19:35












  • The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
    – rd_nielsen
    Nov 11 at 19:37










  • @rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
    – vivek_23
    Nov 11 at 19:48










  • @vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
    – rd_nielsen
    Nov 11 at 19:53






  • 1




    @vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
    – rd_nielsen
    Nov 11 at 20:03
















This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
– vivek_23
Nov 11 at 19:35






This wouldn't work. We can't create a new table since we can't fix a menu position. It might be 1 for a particular data set, 2 for another etc.
– vivek_23
Nov 11 at 19:35














The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
– rd_nielsen
Nov 11 at 19:37




The accessory table only needs the integers to be in ascending order, not sequential. The window function will ensure that the numbers are sequential.
– rd_nielsen
Nov 11 at 19:37












@rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
– vivek_23
Nov 11 at 19:48




@rd_nielsen Please tag. What do you mean by the integer order of those sizes in the second column ? If the window function ensures it's sequential order, why create a separate table in the DB? I think we could have the same table with a different name self joined with managing integer order of those sizes as their id itself(since it would be in ascending order anyway).
– vivek_23
Nov 11 at 19:48












@vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
– rd_nielsen
Nov 11 at 19:53




@vivek_23 "By integer order of those sizes," I mean that if "S" is to sort before "M", then the integers assigned to "S" and "M" should reflect that order--that is, the integer for "S" is smaller than the integer for "M". If you create a separate table for this information, then your data are all in the database, not partly in the database and partly in your interface code.
– rd_nielsen
Nov 11 at 19:53




1




1




@vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
– rd_nielsen
Nov 11 at 20:03




@vivek_23 Because "L" and "40" are for different product IDs. With an accessory table, Table1 doesn't need the Menu_position column at all. The accessory table of size labels and menu position ordering integers would have very few rows, but accomplish the same purpose.
– rd_nielsen
Nov 11 at 20:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252030%2fhow-can-you-update-a-table-with-an-ascending-order-position-based-on-different-g%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)