I have a windows function (over
, partitioned by
) in my code:
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
but apparently this isn't supported on MariaDB before 10.2 (-I am using 10.1). Could someone please suggest alternative code which is both efficient and works on MariaDB 10.1 also?
dbfiddle provided, unfortunately with only MariaDB 10.2 as the oldest; can't test 10.1 directly here
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<>fiddle here