How to get the available area codes? - php

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

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.

something like this?
select distinct substring(number, 0, 3) as 'number'
from numbers
order by number

Related

Sorting of double values from database

In the database I keep records to which codes are assigned (double format).
When calling records from the database I would like them to be sorted from the smallest to the largest one.
The problem arises when 1.2 > 1.10 which is understandable.
I would like it to be interpreted as follows: 1.10 > 1.2
I tried to do this by changing the type to TEXT.
Adding spaces at the end of the "code".
I have an idea to divide the numbers into segments and if the first segment is equal then sort by the second segment.
It seems logical, but I don't know how to translate it into code.
Could anyone help?
if your codes contain a valid decimal value the you could try converting the codes in order by as couple of integer
select codes
from my_table
order by ( CAST(left(codes, locate('.', codes)-1 ) AS UNSIGNED),
CAST(right(codes, length(codes -locate('.', codes)) AS UNSIGNED))

Regular Expression where a number is less than 10

I have a table with a column where I have data stored this way:
1:29,3:20,5:0,4:0,2:76
I want to make a query request in PHP (or MySQL in general) and get rows where there is a number less than 10 after the " : ".
Therefore, in this case I have ( 29 , 20 , 0 , 0 , 76 ), and because I have a number less than 10, I will want to take this row.
In general, storing comma separated values is not considered a good design in SQL, as it leads to violation of data normalization rules and in addition faces many other issues. But since you have already done it and want a regex solution to your problem, you can use this regex,
:[0-9](,|$)
Here is a demo
In mysql you can write a query like this,
select * from tablename where columnname regexp ':[0-9](,|$)';
This will give you rows where at least one comma separated value is less than ten following a colon.

MySQL sort within one column of data

For example, if I had a column in a database called RANDOM that has random bits of information distinguished by their end notation like this:
RANDOM
1. 12312 KM, 201 M, 1213 H, 101029 DOLLARS
2. 231 KM, 2351 M, 754 H, 345 DOLLARS, 120 L, 1201 FT
3. 2324 M
Some entries have other miscellaneous but important data points while others my only have one or two.
I would like to sort using only data within column RANDOM.
$RESULT = mysqli_query($CON, "SELECT * FROM TABLE WHERE RANDOM CONTAINS 'M' ORDER BY 'NUMBER BEFORE M'");
Therefore this would find the 3 rows that contain 'M' and then sort by the number in front of 'M'. Similarly with other variables like KM or DOLLARS. Is this possible using pure MySQL in a single statement?
Your sort would require a 2 aspirin headache with
ORDER BY substr('str',locate(str,a,b),locate(str,b,c))
If I understand the question then yes you could do it in one line , just requires some manipulation as your data is all in the one column.
SELECT * FROM
(SELECT *, CAST(LEFT(RANDOM, INSTR(RANDOM, '.') - 1) AS UNSIGNED) AS ORD_NUMBER
FROM `test_table`
) DERIVED_TABLE ORDER BY ORD_NUMBER DESC
Basically we are getting the number at the start by using INSTR to find the first . ( assuming this is the format for all items) and then to be safe we cast that as an integer. The data is returned using a derived table so then we sort on our dynamically calculated ord_number that's the result from filtering. Hope that helps.
So using this principle you can filter out however you like :)
But maybe rethink the db Design as doing anykind of query like this means maybe your RDBMS isn't setup correct (simple solution new column with that value :))

Storing Date Range in MySQL Solution

I am working on script which requires giving the admin the ability to insert dates for when he wants a parking lot available, the admin inserts dates in a range.
I am having a hard time coming to a solution to what would be the best way to store the dates in MySQL.
Should i store the dates using two columns AVAILABLE_FROM_DATE and AVAILABLE_UNTIL_DATE?
PLID AVAILABLE_FROM DATE AVAILABLE_UNTIL_DATE
1 2012-04-01 2012-04-03
1 2012-04-05 2012-04-15
2 2012-04-21 2012-04-30
OR should i just use a single column AVAILABLE_DATE and store the ranges the admin selects in a new row for each date between the range?
[EDIT START]
What i mean above by using a single column is not to join or split the dates into a single column, i actually mean to store a date in a single row with a single column like below:
PLID AVAILABLE_DATE
1 2012-04-01
1 2012-04-02
1 2012-04-03
and so on for all the available dates i want to store.
[EDIT END]
Basically, the admin will want to insert a date range the parking lot is available and allow members to choose that slot if the user is looking for a slot within that range.
OR is there some better and simpler way to do this?
I am currently trying to use the first method using separate columns for the range, but having trouble getting the desired results when looking for parking lots within a range.
[EDIT START]
SELECT * FROM `parking_lot_dates`
WHERE (available_from_date BETWEEN '2012-04-22' AND '2012-04-30'
AND (available_until_date BETWEEN '2012-04-22' AND '2012-04-30'))
I use the following query on the above rows i have, and it returns empty.
I want it to return the last row having the PLID 2.
[EDIT END]
Thank you in advance.
Regarding your EDIT with the query, you have the logic inside out. You need to compare whether each date you are checking is inside the range BETWEEN available_from_date and available_until_date, like this:
SELECT * FROM `parking_lot_dates`
WHERE
(
'2012-04-22' BETWEEN available_from_date AND available_until_date
AND '2012-04-30' BETWEEN available_from_date AND available_until_date
)
Demo: http://www.sqlfiddle.com/#!2/911a3/2
Edit: Although if you'll want to allow partial-range matches, you'll need both types of logic, i.e., the parking lot is available 4-22 to 4-27, and you need it 4-23 to 4-28. You can use it for the dates 4-23 to 4-27, but not 4-28.
Why to complicate so much?
SELECT *
FROM `parking_lot_dates`
WHERE available_from_date <= '2012-04-22'
AND available_until_date >= '2012-04-30';
I personally have found it better to have 2 columns, a start and end time, for searching a specific date, or just looking at it seems easier to me
Using 1 column to store those dates is a bad design from a database point of view (not normalized). It's better to have 2 columns because the results can be retrieved easier and extracting the information from a single column would mean having to do some sort of split. It's just not elegant and it doesn't behave well when requirements change.

Sort MySQL in Human

here's the code:
$sql_namesResult = mysql_query("SELECT name FROM `scrimaprovedlist` ORDER BY `scrimaprovedlist`.`eorank`");
eo rank is a NUMERICAL value for a rank (general, colonel, ect).
The problem is, when i set myself for 1, i am the top, but comes rank 10, instead of rank 2. how do i edit this to make it show in order:
1
2
3
10
20
30
I'm currently using "rank" instead of "eorank" because it is easier. but the problem is i have to manually edit the ranks over and over again so that they show in the correct order. Any ideas?
Viewable at http://www.thexcrew.com/modules.php?name=Roster
ORDER BY CAST(scrimaprovedlist.eorank AS INTEGER)
Your ranks are strings instead of integers so they will be sorted as a string unless you cast or convert them to integers which I've done above
figured out a way, i changed my Numerical value to an alphabetical value. using only 17 ranks, i am able to substitute #'s for letters. thanks for the help anyway :)

Categories