Re: Distinct Partial Matches: RegExp
| From: | Micah Stevens | Date: | Thu, 31 Aug 2006 00:36:55 +0000 |
| Subject: | Re: Distinct Partial Matches: RegExp | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-42308@lists.php.net to get a copy of this message | ||
Oh! I misunderstood..
This is tougher, you're selecting the entire set, so you won't have anything after 'WHERE', but you're defining how the returned items are formatted, this goes before table selection:
Select DISTINCT SUBSTRING_INDEX(
area, '-', 2) from table;
I didn't test this, but it should work. Look up SUBSTRING_INDEX in the docs..
-Micah
Kevin Murphy wrote:
Well, its not really a search.... that would be way easier. :-) What I'm looking for is a query that will give me the complete list of items that are distinct, minus the last number after the last hyphen. animal-dog animal-cat animal-bird --Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 On Aug 30, 2006, at 4:34 PM, Micah Stevens wrote:Select DISTINCT area from table like '$searchterms%'; In SQL, you can use the 'LIKE' keyword along with the '%' and '_' wildcards.. '_' is one character, '%' is any number of chars. -Micah Kevin Murphy wrote:This might be really easy, but I'm just not sure how to write this query and my searching on google isn't finding me things, probably because I am searching for the wrong terms. I have a bunch of records where the "area" column is like: animal-dog-5 animal-dog-3 animal-cat-1 animal-cat-22 animal-bird-5 What I want to do is run a distinct query on just the part previous to the number. animal-dog animal-cat animal-bird So in other words, something like this, but I am not sure if this is the right way to go: $query = "SELECT DISTINCT area FROM table WHERE REGEXP 'anynumberofletters dash anynumberofletters dash '" Of course, I could be barking up the wrong tree with the REGEXP thing. Anyone care to point me in the right direction? --Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 --Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326