{"id":284218,"date":"2017-03-31T02:30:02","date_gmt":"2017-03-30T22:30:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=284218"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=284218","title":{"rendered":"\u0427\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0432 MySQL"},"content":{"rendered":"<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<br \/>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0441\u0442\u0430\u043b\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0434\u0430\u043d\u043e:<br \/>  \u0414\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u2014 <b>shops<\/b> \u0438 <b>products<\/b><\/p>\n<p>  \u0413\u0440\u0443\u0431\u043e \u0433\u043e\u0432\u043e\u0440\u044f \u2014 \u043f\u043b\u043e\u0449\u0430\u0434\u043a\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u044b \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u044e\u0442 \u0441\u0432\u043e\u0438 \u0442\u043e\u0432\u0430\u0440\u044b.<br \/>  \u0418 \u0432\u043e\u0442, \u0432\u0441\u0442\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0432\u044b\u0434\u0430\u0447\u0443 \u0442\u043e\u0432\u0430\u0440\u043e\u0432, \u043d\u043e \u0442\u0430\u043a, \u0447\u0442\u043e\u0431 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0432\u0438\u0434\u0435\u043b \u043a\u0443\u0447\u0443 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430. \u041c\u0430\u0433\u0430\u0437\u0438\u043d\u044b \u043d\u0430\u0434\u043e \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u0442\u044c.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  <b>shops:<\/b>  <\/p>\n<ul>\n<li>id int not null auto_increment primary key,<\/li>\n<li>name varchar(255) null<\/li>\n<\/ul>\n<p>  <b>products:<\/b>  <\/p>\n<ul>\n<li>id int not null auto_increment primary key,<\/li>\n<li>shop_id int not null,<\/li>\n<li>name varchar(255) null,<\/li>\n<\/ul>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0433\u0443\u0433\u043b\u0438\u0432 \u2014 \u0442\u043e\u043b\u043a\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043d\u0435 \u0431\u044b\u043b\u043e. \u041d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043c\u044b\u0441\u043b\u044c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0441 \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432.<\/p>\n<p>  \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0438\u0448\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:  <\/p>\n<ol>\n<li>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0443.<\/li>\n<li>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0442\u043e\u0432\u0430\u0440, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 1 \u0441 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u043c, \u0440\u0430\u0432\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432.<\/li>\n<li>\u041f\u0440\u0438 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u0432\u0430\u0440\u044b \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u2014 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u043e\u043b\u044c, \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443, \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u043d\u043e\u0432\u0430<\/li>\n<li>\u0412\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u044b, \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0438\u0445 \u043f\u043e \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e<\/li>\n<\/ol>\n<p>  \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 MySQL. \u0418 \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0434\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c.<\/p>\n<p>  \u0421\u043b\u043e\u0436\u0438\u0432 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043f\u043b\u0430\u043d \u2014 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<br \/>  \u0427\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f?  <\/p>\n<ol>\n<li><b>@i<\/b> \u2014 \u0421\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0438 \u0442\u043e\u0432\u0430\u0440\u044b<\/li>\n<li><b>@cnt<\/b> \u2014 \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432<\/li>\n<li><b>@delta<\/b> \u2014 \u0434\u0435\u043b\u044c\u0442\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043f\u0440\u0438 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430<\/li>\n<li><b>@cur<\/b> \u2014 id \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430, \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0435\u043b\u044c\u0442\u044b \u0438 \u0441\u0431\u0440\u043e\u0441\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u043f\u0440\u0438 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430<\/li>\n<\/ol>\n<p>  \u041e\u0431\u044a\u044f\u0432\u0438\u043c \u043d\u0430\u0448\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:  <\/p>\n<pre><code class=\"sql\">set @cnt = 0; set @i = 0; set @delta = 0; set @cur = 0;<\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u043a\u043e\u043b-\u0432\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432 \u0438 id \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430)  <\/p>\n<pre><code class=\"sql\">select @cur:=id from shops order by id limit 1; select @cnt:=count(id) from shops;<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0441\u0430\u043c\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435. \u0427\u0442\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e?  <\/p>\n<ul>\n<li>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u044b \u0441 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u043c, \u0440\u0430\u0432\u043d\u044b\u043c \u043a\u043e\u043b-\u0432\u0443 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432.<\/li>\n<li>\u041f\u0440\u0438 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u2014 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0435\u043b\u044c\u0442\u0443 \u0438 \u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d.<\/li>\n<\/ul>\n<p>  \u0423 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:  <\/p>\n<pre><code class=\"sql\">select id, shop_id, @i:=@i+@cnt as counter,                      IF(@cur&lt;&gt;shop_id,@delta:=@delta+1,@delta) as delta,                      IF(@cur&lt;&gt;shop_id,@i:=@delta,@i) as cur,                      IF(@cur&lt;&gt;shop_id,@cur:=shop_id,@cur) as curshop   from t_product order by shop_id<\/code><\/pre>\n<p>  \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0447\u0442\u043e:<\/p>\n<pre><code class=\"sql\">@i:=@i+@cnt<\/code><\/pre>\n<p>  \u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0448 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430 \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u043e\u0435 \u043a\u043e\u043b-\u0432\u0443 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432.<br \/>  \u0422.\u0435., \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 5 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, \u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u044f: 0, 5, 10, 15 \u0438 \u0442.\u0434.<\/p>\n<pre><code class=\"sql\">IF(@cur&lt;&gt;shop_id,@delta:=@delta+1,@delta) as delta<\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d \u2014 \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0434\u0432\u0438\u0433 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u0422.\u0435. \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0441\u0434\u0432\u0438\u0433 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d 0, \u0434\u043b\u044f \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u2014 1 \u0438 \u0442.\u0434.<\/p>\n<pre><code class=\"sql\">IF(@cur&lt;&gt;shop_id,@i:=@delta,@i) as cur,<\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0441\u043c\u0435\u043d\u0435 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u043d\u0430\u043c \u0442\u0430\u043a-\u0436\u0435 \u043d\u0430\u0434\u043e \u0441\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0430\u0448 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u044b \u0441 \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u0435 \u0437\u0430\u0431\u044b\u0432 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0434\u0432\u0438\u0433.<\/p>\n<pre><code class=\"sql\">IF(@cur&lt;&gt;shop_id,@cur:=shop_id,@cur) as curshop<\/code><\/pre>\n<p>  \u0418 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432 \u2014 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0442\u043e\u0432\u0430\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043d\u0443\u043c\u0435\u0440\u0443\u0435\u043c\u2026<\/p>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0442\u0438\u043f\u0430:<\/p>\n<table border=\"1\">\n<tr>\n<th>id<\/th>\n<th>shop_id<\/th>\n<th>counter<\/th>\n<th>delta<\/th>\n<th>cur<\/th>\n<th>curshop<\/th>\n<\/tr>\n<tr>\n<td>43989<\/td>\n<td>1<\/td>\n<td>10<\/td>\n<td>0<\/td>\n<td>10<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>46989<\/td>\n<td>1<\/td>\n<td>20<\/td>\n<td>0<\/td>\n<td>20<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>114172<\/td>\n<td>1<\/td>\n<td>30<\/td>\n<td>0<\/td>\n<td>30<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>83989<\/td>\n<td>1<\/td>\n<td>40<\/td>\n<td>0<\/td>\n<td>40<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>67172<\/td>\n<td>1<\/td>\n<td>50<\/td>\n<td>0<\/td>\n<td>50<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>94672<\/td>\n<td>2<\/td>\n<td>11<\/td>\n<td>1<\/td>\n<td>11<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>6489<\/td>\n<td>2<\/td>\n<td>21<\/td>\n<td>1<\/td>\n<td>21<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>41989<\/td>\n<td>2<\/td>\n<td>31<\/td>\n<td>1<\/td>\n<td>31<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>61672<\/td>\n<td>2<\/td>\n<td>41<\/td>\n<td>1<\/td>\n<td>41<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>97489<\/td>\n<td>3<\/td>\n<td>12<\/td>\n<td>2<\/td>\n<td>12<\/td>\n<td>3<\/td>\n<\/tr>\n<\/table>\n<p>  \u0422\u0443\u0442 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e, \u043f\u0440\u0438 \u0441\u043c\u0435\u043d\u0435 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u043e\u043d \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0434\u0432\u0438\u0433 \u0438 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043d\u0430\u0447\u0430\u043b\u0430 (\u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0441\u0434\u0432\u0438\u0433\u0430).<\/p>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u0435\u043b\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430 \u043c\u0430\u043b\u044b\u043c. \u041e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0432 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0443:<\/p>\n<pre><code class=\"sql\">select id as product_id, shop_id, cur from (     select id, shop_id, @i:=@i+@cnt as counter,         IF(@cur&lt;&gt;shop_id,@start:=@delta+1,@delta) as delta,         IF(@cur&lt;&gt;shop_id,@i:=@delta,@i) as cur,         IF(@cur&lt;&gt;shop_id,@cur:=shop_id,@cur) as curmag         from  products order by shop_id     ) as A order by cur ;<\/code><\/pre>\n<p>  \u0412\u0443\u0430\u043b\u044f! \u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u0432\u044b\u0431\u043e\u0440\u043a\u0430 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0441 \u0447\u0435\u0440\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u043c\u0438:<\/p>\n<table border=\"1\">\n<tr>\n<th>product_id<\/th>\n<th>shop_id<\/th>\n<th>cur<\/th>\n<\/tr>\n<tr>\n<td>4187<\/td>\n<td>1<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>7483<\/td>\n<td>2<\/td>\n<td>11<\/td>\n<\/tr>\n<tr>\n<td>4045<\/td>\n<td>3<\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td>9091<\/td>\n<td>4<\/td>\n<td>13<\/td>\n<\/tr>\n<tr>\n<td>1457<\/td>\n<td>5<\/td>\n<td>14<\/td>\n<\/tr>\n<tr>\n<td>2387<\/td>\n<td>6<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>8109<\/td>\n<td>7<\/td>\n<td>16<\/td>\n<\/tr>\n<tr>\n<td>1445<\/td>\n<td>8<\/td>\n<td>17<\/td>\n<\/tr>\n<tr>\n<td>2102<\/td>\n<td>9<\/td>\n<td>18<\/td>\n<\/tr>\n<tr>\n<td>9245<\/td>\n<td>10<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>6744<\/td>\n<td>1<\/td>\n<td>20<\/td>\n<\/tr>\n<tr>\n<td>7854<\/td>\n<td>2<\/td>\n<td>21<\/td>\n<\/tr>\n<tr>\n<td>2164<\/td>\n<td>3<\/td>\n<td>22<\/td>\n<\/tr>\n<\/table>\n<p>  \u0415\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043c\u0438\u043d\u0443\u0441 \u2014 \u0442\u043e\u0432\u0430\u0440\u044b \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443. \u0422.\u0435. \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u043e\u0432\u0430\u0440 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430, \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u043e\u0432\u0430\u0440 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430, \u0442\u0440\u0435\u0442\u044c\u0435\u0433\u043e, \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0433\u043e \u0438 \u0442.\u0434. \u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u043e\u0439\u0434\u0443\u0442 \u0432\u0442\u043e\u0440\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, \u0442\u0440\u0435\u0442\u044c\u0438 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>  \u0414\u0430\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0439 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u044b \u0432 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435, \u043e\u0431\u0435\u0440\u043d\u0443\u0432 \u0435\u0435 \u0432 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441:  <\/p>\n<pre><code class=\"sql\">select id as product_id, shop_id, cur from (     select id, shop_id, @i:=@i+@cnt as counter,         IF(@cur&lt;&gt;shop_id,@start:=@delta+1,@delta) as delta,         IF(@cur&lt;&gt;shop_id,@i:=@delta,@i) as cur,         IF(@cur&lt;&gt;shop_id,@cur:=shop_id,@cur) as curmag         from  products order by shop_id     from (select id, shop_id from products order by shop_id, rand()) as A order by shop_id) as B order by cur;<\/code><\/pre>\n<p>  \u0422\u0430\u043a \u043d\u0430\u0448\u0438 \u0442\u043e\u0432\u0430\u0440\u044b \u0443\u0436\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u043d\u044b \u0435\u0449\u0435 \u0434\u043e \u0438\u0445 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u043e\u0441\u044c.<br \/>  \u041f\u043e\u043b\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043e\u043b\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">set @cnt = 0; set @i = 0; set @start = 0; set @cur = 0;  select @cur:=id from shops order by id limit 1; select @cnt:=count(id) from shops;  select id as product_id, shop_id, cur from (     select id, shop_id, @i:=@i+@cnt as counter,         IF(@cur&lt;&gt;shop_id,@start:=@delta+1,@delta) as delta,         IF(@cur&lt;&gt;shop_id,@i:=@delta,@i) as cur,         IF(@cur&lt;&gt;shop_id,@cur:=shop_id,@cur) as curmag         from  products order by shop_id     from (select id, shop_id from products order by shop_id, rand()) as A order by shop_id) as B order by cur;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0420\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u2014 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u043a\u0438. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u0435:<br \/>  10 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, 10000 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u2014 ~16ms (0.016s)<br \/>  100 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, 1000000 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u2014 ~2568ms (2.568s)<br \/>  100 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, 10000000 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u2014 129951ms (2m 9.951s)<\/p>\n<p>  \u042f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u0445\u043e\u0442\u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430\u0434\u043e \u043f\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u043e\u0435\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435.<\/p>\n<p>  <i>P.S. \u0414\u043b\u044f \u043c\u0435\u043d\u044f \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043d\u0435\u0432\u044b\u044f\u0441\u043d\u0435\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441.<br \/>  \u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438?<br \/>  \u0412\u0435\u0434\u044c \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u2014 \u044d\u0442\u043e \u043d\u043e\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. <br \/>  \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u043d\u043d\u044b\u0435 \u0432 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u0445 \u0442\u043e\u0432\u0430\u0440\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u043d\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437.<br \/>  \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043c\u044b\u0441\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0441\u0447\u0435\u0442 \u2014 \u0431\u0443\u0434\u0443 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0435\u043d \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0438\u0445 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0445.<\/i><\/p>\n<p>  \u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 =)<br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/325316\/\"> https:\/\/habrahabr.ru\/post\/325316\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442!<br \/>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0441\u0442\u0430\u043b\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0434\u0430\u043d\u043e:<br \/>  \u0414\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u2014 <b>shops<\/b> \u0438 <b>products<\/b><\/p>\n<p>  \u0413\u0440\u0443\u0431\u043e \u0433\u043e\u0432\u043e\u0440\u044f \u2014 \u043f\u043b\u043e\u0449\u0430\u0434\u043a\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u044b \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u044e\u0442 \u0441\u0432\u043e\u0438 \u0442\u043e\u0432\u0430\u0440\u044b.<br \/>  \u0418 \u0432\u043e\u0442, \u0432\u0441\u0442\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0432\u044b\u0434\u0430\u0447\u0443 \u0442\u043e\u0432\u0430\u0440\u043e\u0432, \u043d\u043e \u0442\u0430\u043a, \u0447\u0442\u043e\u0431 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0432\u0438\u0434\u0435\u043b \u043a\u0443\u0447\u0443 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430. \u041c\u0430\u0433\u0430\u0437\u0438\u043d\u044b \u043d\u0430\u0434\u043e \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u0442\u044c.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-284218","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284218"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284218\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}