Jack
Jack

Reputation: 3

PHP & MYSQL - How To Check If Two Or More Rows Have The Same Field Value

So here's the problem I can't work around today.

I want to grab some details from a database. I'm not sure how to explain this. But here goes.

Database Example:

TITLE | DESCRIPTION | IDENTIFIER

1 | Description | abc
2 | Description | abc
3 | Description | def

I want to select the first row with the identifier "abc" and then I want to skip the next row which has the same field "abc".

I'm trying to use this in a SELECT mysql_query in PHP

Upvotes: 0

Views: 980

Answers (5)

Vic Abreu
Vic Abreu

Reputation: 500

select id, description, identifier 
from table group by identifier order by id asc;

Upvotes: 0

Jason
Jason

Reputation: 1962

This?

SELECT DISTINCT(identifier), title, description FROM tablename

It will also return the row with 'def' in this case.

Upvotes: 1

Hắc Huyền Minh
Hắc Huyền Minh

Reputation: 1035

select min(TITLE) as TITLE, DESCRIPTION , IDENTIFIER 
From your_table
Group by DESCRIPTION , IDENTIFIER 

Upvotes: 0

RustProof Labs
RustProof Labs

Reputation: 1277

It appears that the integer field is called 'title'. This should grab the lowest title # but remove the duplicates as you requested.

select min(title) as firstTitle, description, identifier
  from table_name
  where identifier in (select distinct identifier from table_name)
  group by description, identifier.

Upvotes: 0

juergen d
juergen d

Reputation: 204766

To get the complete row you can do

select * from your_table
where title in 
(
  select min(title)
  from your_table
  group by identifier
)

Upvotes: 1

Related Questions