5

I have 4 columns a, b c, d in my table (MySQL database) . I want to select the distinct values of ALL of these 4 columns in my table . More deeply my table is given bellow..

a   b   c   d
--------------------------
1   3   3   4
1   2   3   0
1   1   3   4
1   2   3   4
1   2   3   4
1   2   3   4
1   2   3   4

In the above table (1,2,3,4) value are repeating 4 times(look the last 4 rows of my table). I want only the distinct one , ie i want to get the bellow table after mysql query..

a   b   c   d
---------------
1   3   3   4
1   2   3   0
1   1   3   4
1   2   3   4

I think you guys got some idea . Im not familier with MySql .

2
  • 1
    Which one do you want? Note that in the absence of a PRIMARY KEY a 'table' is not really a table in an RDBMS sense. Commented Apr 2, 2014 at 9:48
  • have you tried distinct keyword before the column names? Commented Apr 2, 2014 at 10:07

4 Answers 4

8
select distinct a,b,c,d from your_table
Sign up to request clarification or add additional context in comments.

4 Comments

Its working well..I have 1 more need , ie there is 2 more fields in ma table(e & f). I want to get the corresponding values also.How can i do that??
jsut add select distinct a,b,c,d,e,f from your_table
I want to avoid that field from unique condition.If i add that filed also then , does the mysql check the unique condition for that field also??
Yes, it does. But if you don't want those other fields to be unique you have to define a rule which one to take because you can't take all, right? Would be best to ask another question and add your example data and expected output.
2
SELECT DISTINCT column_name,column_name FROM table_name;

I mean

select distinct a,b,c,d from table_name;

Here is the link of w3schools

Comments

0

use : SELECT DISTINCT * FROM yourtable

Comments

0

try this:

SELECT DISTINCT a FROM my_table
UNION 
SELECT DISTINCT b FROM my_table
UNION
SELECT DISTINCT c FROM my_table
UNION
SELECT DISTINCT d FROM my_table

update 1:

SELECT a,b,c,d 
FROM my_table 
GROUP BY a,b,c,d;

http://sqlfiddle.com/#!2/c49ad/11

7 Comments

what would be the output of this query
what is the benefit of using Distinct keywords with Group By?
@Nimesh, if you wants to remove the duplicate means use the distinct or otherwise use the group by like its aggregate operators to each group
and have you checked your first query result in fiddle?
@jmail OP has already suggested the output and your first query result does not match with the suggested output.
|

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.