ipixel
ipixel

Reputation: 529

MySQL - Selecting DISTINCT field with other fields

I have a table..

field_1 | field_2 | field_3 | field_4
data_1  | data_2  | data_3  | data_4

all the data withing tablename can potentially repeat.

I want to select unique field_1's so that i dont get any repeats, but I also need the rest of that row of data.

SELECT DISTINCT field_1, field_2, field_3, field_4 FROM tablename;

The query above obviously does not work. So my question is how can i do this?

Update

How can I prevent the duplicates from displaying.

so if i have

data_1|data_2|data_3|data_4
data_1|data_2|data_3|data_4
data_6|data_7|data_8|data_9

the results will only display

data_6|data_7|data_8|data_9

Upvotes: 1

Views: 4898

Answers (1)

John Woo
John Woo

Reputation: 263883

Can you give more records? The query below will produce unique field_1 but a random record for every group (especially when dealing with large database).

SELECT field_1, field_2, field_3, field_4
FROM   TableName
GROUP  BY field_1

UPDATE 1

SELECT  field_1, field_2, field_3, field_4
FROM    TableName
GROUP   BY field_1, field_2, field_3, field_4
HAVING  COUNT(*) = 1

Upvotes: 3

Related Questions