2

I need one help.I need query to skip duplicate column value according to the auto increment id using MySQL.I am explaining my table structure below.

db_subcat:

member_id      day_id

16               2

18               2

18               2

18               3

Here i need to skip the duplicate column value according to the member_id e.g- here for member_id-18 there are two day_id as 2,i need to fetch only one if member_id is same.

satya
  • 3,508
  • 11
  • 50
  • 130
  • You can use `UNIQUE` key in MySQL so that duplicate results will not be inserted in the first place – Panda Mar 03 '16 at 10:45
  • Possible duplicate of [Delete all Duplicate Rows except for One in MySQL?](http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-except-for-one-in-mysql) – Muhammad Muazzam Mar 03 '16 at 10:49

4 Answers4

2

you can use distinct:

select distinct member_id, day_id from db_subcat;
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
1

you can use distinct as well as group by

select distinct member_id, day_id from db_subcat;
select member_id, day_id from db_subcat group by member_id, day_id;

Here distinct will be faster than group by. To see the difference have a look at http://charlesnagy.info/it/mysql/difference-between-distinct-and-group-by

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
0

You can use the UNIQUE key in MySQL so that the duplicate results will not be inserted in the first place.

Else, you can use SELECT distinct:

SELECT distinct member_id, day_id from db_subcat;

More information on SQL Unique Constraint: http://www.w3schools.com/sql/sql_unique.asp.

More information on SQL Select Distinct: http://www.w3schools.com/sql/sql_distinct.asp.

Panda
  • 6,955
  • 6
  • 40
  • 55
0

Apart from skipping the duplicate member id, you may want the sum of day_id as well, below query fetches that:

select member_id, sum(day_id)
from db_subcat
group by member_id

You can use any aggregate function (e.g. min, max, count, avg) according to the requirement.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102