views:

443

answers:

2

I have a table with playerhandles, like this:

1 - [N] Laka
2 - [N] James
3 - nor | Brian
4 - nor | John
5 - Player 2
6 - Spectator
7 - [N] Joe

From there I wanna select all players where the first n-chars match, but I don't know the pattern, only that it's the first n-chars. In the above example I wan't it to return rows 1,2,3,4 and 7.

Is this possible in MySQL? And not too expensive...

Thanks! =)

+2  A: 
Kieveli
Lasse A Karlsen
Kieveli
Ya, my bad. Fixed.
Kieveli
Btw... could you add a little something to that query? Dismiss all handles which are plain letters? No |,-,[],() or whatever..
Lasse A Karlsen
Hmmm that might get tricky. Look at LOCATE('|', p1.name) != 0 Maybe have one per expression and OR them together to indicate must have at least one of these odd characters.
Kieveli
+4  A: 

If you know the value of n, you could do something like this (for n=3):

Select *
FROM players
WHERE Left(name, 3) in (
   SELECT Left(name, 3)
   FROM players
   GROUP BY Left(name, 3)
   HAVING (Count(*) > 1)
);
foxy
I need a local MySQL install - all I've got is SQL Server, but your query is almost *exactly* what I was going to suggest... :)
GalacticCowboy
Plus it avoids messing with LIKE - player names could contain characters used for matching ('%', '_') so you'd have to escape them.
GalacticCowboy