Tommizzy
Tommizzy

Reputation: 161

Select all first record matching first unique word

I have the following data that looks similar to this:

id        property  position
========  ========  ========
1         Cool      Leader
2         Cool      Sponsor 1
3         Cool      Sponsor 2
4         Cool      Sponsor 3
5         Hot       Icon
6         Hot       Sponsor 2
7         Hot       Sponsor 3

I am attempting to write a query that returns rows of the unique position values but ignores the number after the position. So if Sponsor 1 exists then do not return rows where there is Sponsor 2 and Sponsor 3.

I expect the results to be:

id        property  position
========  ========  ========
1         Cool      Leader
2         Cool      Sponsor 1
5         Hot       Icon
6         Hot       Sponsor 2

How can I do this?

Upvotes: 0

Views: 40

Answers (1)

mona.helal
mona.helal

Reputation: 112

Using MySql

If you want to get the first occurrence of the string

SELECT property, position, SUBSTRING_INDEX(position, " ", 1) AS p2 
FROM table_name 
GROUP BY p2, property;

Upvotes: 1

Related Questions