如何在MIN的SELECT中包含第三列

时间:2019-02-06 22:49:28

标签: sql syntax mariadb groupwise-maximum

在此查询中,我还想在同一查询中选择第三列wp_postmeta.post_id。如果不通过涉及子查询的JOIN附加它,我无法弄清楚该如何做。没有使用子查询,难道没有更简单的方法吗? (如果解决方案有效且不会占用过多资源,则将让步子查询。)

MariaDB 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);
SELECT wp_posts.post_title, MIN(wp_postmeta.meta_value) 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' 
GROUP BY wp_posts.post_title
ORDER BY wp_posts.post_title
post_title   | MIN(wp_postmeta.meta_value)
:----------- | --------------------------:
Apple Pie    |                          10
Banana       |                           2
French Toast |                           1
Jam Pie      |                           8
Shepards Pie |                           9

db <>提琴here

编辑:我对最初的问题不够清楚和明确-我想折叠重复的post_titles并使用与wp_postmeta.post_id对应的MIN(pm.meta_value)

3 个答案:

答案 0 :(得分:1)

您不能只将其包含在const a: A = ....; const b1: B = ....; const b2: B = ....; . . . const bn: B = ....; const c: C = ....; const arr: MyArrayType = [a, b1, b2,...., bn, c]; //compiles const arr2: MyArrayType = [c, b1, b2,...., bn, a]; //doesn't compile 中吗?

GROUP BY

SELECT p.post_title, p.id, MIN(pm.meta_value) FROM wp_postmeta pm JOIN wp_posts p ON pm.post_id = p.id WHERE p.post_type = 'Product' AND pm.meta_key = '_regular_price' GROUP BY p.post_title, p.id ORDER BY p.post_title; post_id相同,因此您最好使用它。

编辑:

如果标题可以重复,则只需使用相关的子查询:

p.id

答案 1 :(得分:1)

如果我正确地跟随了您,则可以使用窗口函数解决您的问题:

SELECT x.post_title, x.id
FROM (
    SELECT wp_posts.post_title, wp_postmeta.id, ROW_NUMBER() OVER (PARTITION BY wp_posts.post_title ORDER BY wp_postmeta.meta_value) rn
    FROM wp_postmeta 
    INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id 
    WHERE wp_posts.post_type = 'Product' AND wp_postmeta.meta_key = '_regular_price'
) x WHERE x.rn = 1;

See this DB Fiddle demo

窗口函数是用于此类用例的方式(如果我理解正确的话)。在效率和可读性方面,它们通常优于其他解决方案。


如果RDBMS不支持窗口功能,一种解决方案是使用具有NOT EXISTS条件的相关子查询,以确保对于给定帖子标题,不存在具有较小元值的记录。

SELECT p.post_title, MIN(m.id)
FROM 
    wp_postmeta m
    INNER JOIN wp_posts p 
        ON  p.id = m.post_id
        AND p.post_type = 'Product' 
WHERE 
    m.meta_key = '_regular_price'
    AND NOT EXISTS (
        SELECT 1
        FROM 
            wp_postmeta m1
            INNER JOIN wp_posts p1
                ON  p1.id = m1.post_id
                AND p1.post_type = 'Product'
        WHERE 
            m1.meta_key = '_regular_price'
            AND p1.post_title = p.post_title
            AND m1.meta_value < m.meta_value
     )
 GROUP BY p.post_title

DB Fiddle demo

注意:我对您的SQL做了一些清理(添加了表别名,并将某些条件移到了相关联接的ON子句中。)

答案 2 :(得分:1)

您可以使用CTE托盘:

WITH T1
AS ( SELECT   post_title, MIN(id) AS ID
     FROM     wp_posts
     GROUP BY post_title )
SELECT   T1.ID, wp_posts.post_title, MIN(wp_postmeta.meta_value)
FROM     wp_postmeta
         JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id
         JOIN T1 ON T1.post_title = wp_posts.post_title
WHERE    wp_posts.post_type = 'Product'
         AND wp_postmeta.meta_key = '_regular_price'
GROUP BY T1.ID,  wp_posts.post_title
ORDER BY wp_posts.post_title;

编辑:

您的表wp_posts应该看起来像这样

Id, post_title, post_type
1, 'Apple Pie','Product'
2, 'French Toast','Product'
3, 'Shepards Pie','Product'
4, 'Jam Pie','Product'
5, 'Plate','Not a Product'
6, 'Bucket','Not a Product'
7, 'Chequebook','Not a Product'
8, 'Banana','Product'

并且比这样的wp_postmeta表数据中的

post_id, meta_value
1,  10
2,  5
3,  9
4,  8
4,  11
5,  12 
6,  10
7,  6
2,  1
2,  1
8,  7
8,  2
8,  2

查询如下

SELECT   wp_postmeta.post_id, wp_posts.post_title, MIN(wp_postmeta.meta_value)
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 = 'REG PRICE'
GROUP BY wp_postmeta.post_id,  wp_posts.post_title
ORDER BY wp_posts.post_title;