4

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.

Query One.

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

Following is image

enter image description here

On image you can see Travel and Accessorises repeated

Second query

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

I also refereed this link Select all columns from rows distinct on one column

6
  • what result do you expect? Commented Jan 2, 2018 at 11:49
  • I want to list of categories that will not repeated for e.g ( travel , accessories etc) but not repeated that i have mentioned in above image Commented Jan 2, 2018 at 11:50
  • So you want to exclude Travel and Accessories? Commented Jan 2, 2018 at 11:51
  • or include them with the largest ID? Commented Jan 2, 2018 at 11:52
  • I want like ( travel, accessorise, gift & flowers , hotel ... etc ) all categories but not repeated duplicate Commented Jan 2, 2018 at 11:53

4 Answers 4

7

Remove offer_id from the select:

SELECT DISTINCT category
FROM tbl_coupon_offer;

If you want one offer_id, then use GROUP BY:

SELECT category, MAX(offer_id)
FROM tbl_coupon_offer
GROUP BY category;
Sign up to request clarification or add additional context in comments.

2 Comments

just change the table spelling coupon to coupan
@Gordon Linoff , when i used 3rd column getting error.
1

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

6 Comments

i also want store image in list
@samirsheikh please check the modification
given error "#1052 - Column 'category' in field list is ambiguous "
sorry, fixed it @samirsheikh
@ C. Geek error: "#1054 - Unknown column 'q.store_image' in 'field list''
|
0
SELECT category , MAX(`offer_id`) , store_image 
FROM  `tbl_coupan_offer` 
GROUP BY `category`  

If multiple data coming then just add DISTINCT after SELECT

Comments

-3

Check your database engine type. It is "InnoDB" or not. If your database and table both existed and if you find this error #1146 Table xxx doesn't exist. Then read more about database engine.

If all is right, then query of Gordon is right.

SELECT `category`, `offer_id`, `store_image`
FROM `tbl_coupon_offer`
GROUP BY `category`

2 Comments

Amit, I also want store_image column in list
then add store_image in query. I just updated query. check it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.