programing

MariaDB 10.1의 창 기능에 대한 대안

megabox 2023. 8. 7. 22:27
반응형

MariaDB 10.1의 창 기능에 대한 대안

창 기능이 있습니다(over,partitioned by) 내 코드에서:

FROM   (SELECT wp_posts.id, 
                                     wp_postmeta.post_id, 
                                     post_title, 
                                     post_type, 
                                     meta_value, 
                                     Row_number() 
                                       OVER( 
                                         partition BY post_title 
                                         ORDER BY wp_postmeta.meta_value) rn 

그러나 MariaDB 10.2 이전 버전에서는 지원되지 않습니다(-10.1 사용).MariaDB 10.1에서도 작동하는 효율적인 대체 코드를 제안해주실 수 있나요?

dbfiddle 제공, 안타깝게도 가장 오래된 MariaDB 10.2와 함께 제공됩니다. 여기서 10.1을 직접 테스트할 수 없습니다.

create table wp_posts (
  ID integer primary key auto_increment,
  post_title varchar(30),
  post_type varchar(30)
);
create table wp_postmeta (
  ID integer primary key auto_increment,
  post_id integer,
  meta_key varchar(30) not null default '_regular_price',
  meta_value integer not null
);
insert into wp_posts (post_title, post_type) values
('Apple Pie','Product'),
('French Toast','Product'),
('Shepards Pie','Product'),
('Jam Pie','Product'),
('Jam Pie','Product'),
('Plate','Not a Product'),
('Bucket','Not a Product'),
('Chequebook','Not a Product'),
('French Toast','Product'),
('French Toast','Product'),
('Banana','Product'),
('Banana','Product'),
('Banana','Product');
insert into wp_postmeta (post_id, meta_value) values
(1,10),
(2,5),
(3,9),
(4,8),
(5,11),
(6,12),
(7,10),
(8,6),
(9,1),
(10,1),
(11,7),
(12,2),
(13,2);
-- Deleting all duplicate products in wp_posts table 
DELETE FROM wp_posts 
WHERE  id IN (SELECT id 
              FROM   (SELECT id, 
                             post_title, 
                             post_type, 
                             meta_value 
                      FROM   (SELECT wp_posts.id, 
                                     wp_postmeta.post_id, 
                                     post_title, 
                                     post_type, 
                                     meta_value, 
                                     Row_number() 
                                       OVER( 
                                         partition BY post_title 
                                         ORDER BY wp_postmeta.meta_value) rn 
                              FROM   wp_postmeta 
                                     JOIN wp_posts 
                                       ON wp_postmeta.post_id = wp_posts.id 
                              WHERE  wp_posts.post_type = 'Product' 
                                     AND wp_postmeta.meta_key = '_regular_price' 
                             ) t 
                      WHERE  t.rn <> 1) AS aliasx); 

db<>여기로 이동

언급URL : https://stackoverflow.com/questions/54560605/alternative-to-window-function-in-mariadb-10-1

반응형