请更正此唯一行选择的实现(删除重复项)

Please correct this implementation of unique row selection (drop duplicates)

我正在尝试 select 来自 JOINed table

的唯一行

(忽略符合条件的重复项)

其中重复由列 post_title

中具有相同值的记录定义

每个重复集中 _meta_value 列中值最小的行将被 select 编辑。如果它是 2 行之间的平局,那么只取具有最低 post_IDID(唯一)的行。

到目前为止,这就是我所拥有的 - 它不起作用(returns 零行) - 虽然意图应该很清楚,但我确定我没有使用正确的功能。

SELECT * FROM (

select wp_posts.ID, wp_posts.post_title, wp_postmeta.post_id, wp_posts.post_type, wp_postmeta.meta_key, wp_postmeta.meta_value
      from wp_postmeta JOIN wp_posts 
ON wp_postmeta.post_ID=wp_posts.ID

WHERE post_type = 'product' AND meta_key = '_regular_price'
    GROUP BY post_title
 ) as alias1
HAVING MIN(meta_value)

ORDER BY post_title

这是在两个 table 的 JOIN 之后请求的最小数据样本:

+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+
| wp_posts.ID | wp_posts.post_title | wp_postmeta.post_id | wp_posts.post_type | wp_postmeta.meta_key | wp_postmeta.meta_value |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+
|           1 | Apple Pie           |                   1 | Product            | _regular_price       |                     10 |
|           2 | French Toast        |                   2 | Product            | _regular_price       |                      5 |
|           3 | Shepards Pie        |                   3 | Product            | _regular_price       |                      9 |
|           4 | Jam Pie             |                   4 | Product            | _regular_price       |                      8 |
|           5 | Jam Pie             |                   5 | Product            | _regular_price       |                     11 |
|           9 | French Toast        |                   9 | Product            | _regular_price       |                     12 |
|          10 | French Toast        |                  10 | Product            | _regular_price       |                     12 |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+

查询应该return:

+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+
| wp_posts.ID | wp_posts.post_title | wp_postmeta.post_id | wp_posts.post_type | wp_postmeta.meta_key | wp_postmeta.meta_value |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+
|           1 | Apple Pie           |                   1 | Product            | _regular_price       |                     10 |
|           2 | French Toast        |                   2 | Product            | _regular_price       |                      5 |
|           3 | Shepards Pie        |                   3 | Product            | _regular_price       |                      9 |
|           4 | Jam Pie             |                   4 | Product            | _regular_price       |                      8 |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+

您可以根据 post_title 的过滤器(where)获取副本,使用 group by 并使 count(*) > 1

select wp_posts.post_title
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
    WHERE post_type = 'product' 
    AND meta_key = '_regular_price'
Group by wp_posts.post_title 
having count(*) > 1

您可以使用

获取这些重复项的最小 ID
select wp_posts.post_title, min(wp_posts.ID)
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
    WHERE post_type = 'product' 
    AND meta_key = '_regular_price'
Group by wp_posts.post_title 
having count(*) > 1

你不应该在查询中添加列..这是mysql的某些版本会引发错误,而在其他版本中会产生不可预测的结果
相反,您应该使用带有 min(id) 的聚合结果作为子查询来加入您需要的值

select wp_posts.ID
    , wp_posts.post_title
    , wp_postmeta.post_id
    , wp_posts.post_type
    , wp_postmeta.meta_key
    , wp_postmeta.meta_value
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID 
inner join  (
 select wp_posts.post_title, min(wp_posts.ID) min_id 
    from wp_postmeta 
    JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
        WHERE post_type = 'product' 
        AND meta_key = '_regular_price'
    Group by wp_posts.post_title 
    having count(*) > 1

) t ON t.min_id  = wp_posts.ID  
        and  t.post_title = wp_posts.post_title 

但是如果您希望 post_title 的所有第一个值独立于标题是否重复(如您的示例中所示),那么请避免使用 HAVING count(*) >1

select wp_posts.ID
, wp_posts.post_title
, wp_postmeta.post_id
, wp_posts.post_type
, wp_postmeta.meta_key
, wp_postmeta.meta_value
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID 
inner join  (
  select wp_posts.post_title, min(wp_posts.ID) min_id 
  from wp_postmeta 
  JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
    WHERE post_type = 'product' 
    AND meta_key = '_regular_price'
  Group by wp_posts.post_title 


 ) t ON t.min_id  = wp_posts.ID  
    and  t.post_title = wp_posts.post_title 

似乎问题是将 respect min(wp_posts.ID) 更改为 p_postmeta.meta_value

select wp_posts.ID
, wp_posts.post_title
, wp_postmeta.post_id
, wp_posts.post_type
, wp_postmeta.meta_key
, wp_postmeta.meta_value
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID 
inner join  (
  select wp_posts.post_title, min(wp_postmeta.meta_value) min_val
  from wp_postmeta 
  JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
    WHERE post_type = 'product' 
    AND meta_key = '_regular_price'
  Group by wp_posts.post_title 


 ) t ON t.min_val  = wp_postmeta.meta_value
    and  t.post_title = wp_posts.post_title 

并排除相同 meta_value 的第二行 使用最小值(id)

select wp_posts.ID
, wp_posts.post_title
, min( wp_postmeta.post_id)
, wp_posts.post_type
, wp_postmeta.meta_key
, wp_postmeta.meta_value
from wp_postmeta 
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID 
inner join  (
  select wp_posts.post_title, min(wp_postmeta.meta_value) min_val
  from wp_postmeta 
  JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
    WHERE post_type = 'product' 
    AND meta_key = '_regular_price'
  Group by wp_posts.post_title 


 ) t ON t.min_val  = wp_postmeta.meta_value
    and  t.post_title = wp_posts.post_title 
 group by  wp_posts.ID
, wp_posts.post_title
, wp_posts.post_type
, wp_postmeta.meta_key
, wp_postmeta.meta_value

您可以通过简单的 GROUP BY 和使用 MIN 获得这些结果。

SELECT 
 MIN(p.ID) AS ID, 
 p.post_title, 
 MIN(pm.post_id) AS post_id, 
 p.post_type, 
 pm.meta_key, 
 MIN(pm.meta_value) AS meta_value
FROM wp_posts p 
JOIN wp_postmeta pm ON (pm.post_ID = p.ID AND pm.meta_key = '_regular_price')
WHERE p.post_type = 'product'
GROUP BY p.post_title, p.post_type, pm.meta_key
ORDER BY ID;

db<>fiddle here

的测试