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

时间:2019-01-26 10:57:56

标签: mysql mysql-5.7

我正在尝试从JOINed表中选择唯一行

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

其中重复项是由post_title列中具有相同值的记录定义的。

将选择每个重复集中_meta_value列中具有最小值的行。如果这是2行之间的平局,那么只需采用最低post_IDID(唯一)的行即可。

到目前为止,这就是我所拥有的-它不起作用(返回零行)-尽管意图应该很明确,但我确定我没有使用正确的函数。

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

这是两个表的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 |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+

查询应返回:

+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+
| 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 |
+-------------+---------------------+---------------------+--------------------+----------------------+------------------------+

2 个答案:

答案 0 :(得分:1)

您可以使用group by并让count(*)> 1

根据post_title的过滤器(位置)获得重复项
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

您可以使用

获得这些重复项的最低编号
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 

问题似乎在于将对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 使用min(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

答案 1 :(得分:1)

您可以通过简单的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 <>小提琴here

的测试
相关问题