📜 ⬆️ ⬇️

Sort inside union

Feature sorting inside the union.
thanks funca post leave - can someone come in handy the main point is " Sort the result, not the intermediate data ."
  1. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  2. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  3. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  4. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  5. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  6. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  7. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
  8. CREATE TABLE `t1` ( `id1` int ( 10 ) unsigned NOT NULL default '0' , `id2` int ( 10 ) unsigned NOT NULL , `k1` decimal ( 6, 5 ) unsigned NOT NULL default '0.00000' , `k2` int ( 10 ) unsigned NOT NULL default '0' , PRIMARY KEY ( `id1` , `id2` ) , KEY `k2` ( `id1` , `k2` ) ) ENGINE = MEMORY;
.


engine memory is not the key point - I checked the same thing on MyISAM

further fill it with data similar to the following.
  1. for ( $ i = 0; $ i <100; $ i ++) {
  2. $ i -> query ( 'REPLACE INTO `t1` (` id1`, `id2`,` k1`, `k2`) VALUES (? d,? d,? f,? d)' ,
  3. rand (0.99999), rand (0.5), rand (2.99999) / 50,000, rand (0.20)
  4. );
  5. }
* This source code was highlighted with Source Code Highlighter .

')
and look at the result of such a query:

  1. (
  2. SELECT `id1`,` k1`, `k2`
  3. FROM `t1`
  4. WHERE `id2` = 3 AND` k2` = 13
  5. ORDER BY `k1` DESC
  6. )
  7. UNION (
  8. SELECT `id1`,` k1`, `k2`
  9. FROM `k1`
  10. WHERE `id2` = 3 AND` k2`! = 13
  11. ORDER BY `k1` DESC
  12. )


here it is, for the above example:
 id1 k1 k2

 2726 1.50194 13
 88207 0.25084 13
 37274 0.96550 11
 11059 0.42600 6
 11139 1.90196 4
 63593 1.42970 5
 65273 1.44950 18
 28721 0.79328 15
 70946 0.87576 4
 96673 1.71290 14
 49207 1.92928 17
 40697 1.82320 18


The bug in our opinion is that in the sample generated by the second union request there is no sorting by the middle column.
Anyone from the community can shed light on this behavior?

By the way, of course, we also want to share a solution to this problem:
  1. SELECT `id1`,` k1`, `k2`
  2. FROM `t1`
  3. WHERE id2` = 3
  4. ORDER BY if (`k2` = 13, 0, 1),` k1` DESC


 2726 1.50194 13
 88207 0.25084 13
 49207 1.92928 17
 11139 1.90196 4
 40697 1.82320 18
 96673 1.71290 14
 65273 1.44950 18
 63593 1.42970 5
 37274 0.96550 11
 70946 0.87576 4
 28721 0.79328 15
 11059 0.42600 6

Source: https://habr.com/ru/post/65874/


All Articles