CodeOverload
CodeOverload

Reputation: 48495

How to get the available area codes?

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

Upvotes: 3

Views: 148

Answers (2)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181340

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.

Upvotes: 5

J Benjamin
J Benjamin

Reputation: 4782

something like this?

select distinct substring(number, 0, 3) as 'number' from numbers order by number

Upvotes: 2

Related Questions