code_junkie

Selecting rows where first n-chars are equal (MySQL)

Nov 12th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. 1 - [N] Laka
  2. 2 - [N] James
  3. 3 - nor | Brian
  4. 4 - nor | John
  5. 5 - Player 2
  6. 6 - Spectator
  7. 7 - [N] Joe
  8.  
  9. select name from players p1 where exists (
  10. select 1 from players p2 where
  11. p2.name like CONCAT( SUBSTRING(p1.name, 1, 3), '%')
  12. and p1.name <> p2.name )
  13.  
  14. Select *
  15. FROM players
  16. WHERE Left(name, 3) in (
  17. SELECT Left(name, 3)
  18. FROM players
  19. GROUP BY Left(name, 3)
  20. HAVING (Count(*) > 1)
  21. );
Add Comment
Please, Sign In to add comment