This is a pretty complex MySQL query for me.

what i have is a table of phone numbers in the format of a US phone number 8153216458.

Example:

TABLE: numbers(number)
4512163215
4512158211
4512110579
8812163215
9405462136
3021548641

What i want is to list the available area codes ( as i'm selling numbers ) without repeating them, Some query that is based on the first 3 digits & finally ordered correctly.

Output:
302
451
881
940

Any solution? i don't mind if it's still using php manipulation.

Thanks

Accepted Answer

Try this:

select distinct substring(number, 1, 3)
  from numbers;

Or, as mentioned by Jeff in the comments, left is also an option:

select distinct left(number, 3)
  from numbers;

Check out documentation for string functions on MySQL.

Written by Pablo Santa Cruz
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki