SELECT DISTINCT 在一列上,在 mysql 中有多个列 [英] SELECT DISTINCT on one column, with multiple columns in mysql

查看:74
本文介绍了SELECT DISTINCT 在一列上,在 mysql 中有多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过选择多列来显示结果,但只有一列不同,我还使用了以下查询并参考了以下链接.

I have to display result with select multiple column but distinct on only one column i have also used following queries and also refereed below link.

查询一.

select category , offer_id, store_image from  `tbl_coupan_offer` where
`offer_id` in (Select max(`offer_id`) FROM  `tbl_coupan_offer` group by `category`)  

以上查询返回包含重复的所有记录

Above queries return all record with including duplicate

我只想显示不同的类别而不重复

I want only display distinct category not repeated

以下是图片

在图片上,您可以看到重复的旅行和配饰

On image you can see Travel and Accessorises repeated

第二次查询

SELECT DISTINCT `category`,`offer_id`,`store_image` FROM `tbl_coupan_offer` 

我也参考了这个链接 从不同的行中选择所有列在一列上

推荐答案

就像 Gordon 说的,但是作为一个子查询来获取适当的图像.

Like Gordon said but as a subquery to get the appropriate image.

SELECT DISTINCT q.category, ch_offer_id, store_img
FROM
(
    SELECT category, MAX(offer_id) ch_offer_id
    FROM tbl_coupan_offer
    GROUP BY category
) q
JOIN tbl_coupan_offer
ON q.ch_offer_id=tbl_coupan_offer.offer_id

这篇关于SELECT DISTINCT 在一列上,在 mysql 中有多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆