Cannot get number to increment above 10 [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ok, so a strange one that I've never seen before. The bit of script you are about to see gets a value from a table, explodes it and then increments the number stored in the array, so I can then use it to create a unique product code. However, the number refuses to increment once it hits 10. Here is the script:
$query = $this->mysqli_link->query("SELECT custom_id FROM sjs_custom_orders ORDER BY custom_id DESC LIMIT 1")->fetch_array();
$custom_id_old = $query['custom_id'];
$custom_id_new_split = explode("/", $custom_id_old);
$custom_id = $custom_id_new_split[2] + 1;
$custom_id_new = "SJS/CUSTOM/" . $custom_id;
The table itself has the custom_id column set as a varchar(100).
Any help appreciated.

The problem is in your query.
ORDER BY custom_id DESC
Because the (unfortunately named) custom_id column is character type, this value:
SJS/CUSTOM/9
will sort before (in descending order) this value:
SJS/CUSTOM/10
because the comparison is character to character. The '9' is greater than '1'. As humans, we may perceive ten to be greater than nine. But MySQL is just comparing character to character. If we added a '0' character before the '9', so the database was comparing '09' to '10', we'd be closer to getting the result we expect.
If the value of custom_id always contains two forward slashes, and you want the numeric value after the last forward slash, you can parse that out with a SQL expression
SUBSTRING_INDEX(custom_id,'/',-1)+0
(Add a zero converts the string to numeric value. MySQL is wonky the way it does that, it won't throw an error, it converts the leading portion, as much as it can, into a numeric.
If there's always two slashes, you can get the part before that second slash with an expression as well:
SUBSTRING_INDEX(custom_id,'/',2)
To get the "maximum" value, you could do this...
ORDER
BY SUBSTRING_INDEX(custom_id,'/',2) DESC
, SUBSTRING_INDEX(custom_id,'/',-1)+0 DESC
LIMIT 1
NOTE: This "works" as long as custom_id always contains two forward slashes. If there's any values of custom_id that aren't like that, the behavior of these expressions is well defined, but unlikely to return the result you want.
MySQL will not be able to make effective use of an index to satisfy this query; those expressions will need to be evaluated for every row in the table. If you're stuck with this unfortunate design, then you're stuck with it. We'd prefer to see the results from these expressions stored separately, in two separate columns.

Related

How should I correct ill-formatted lat-long pairs to be valid for PHP computation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.

How to display the column value descending when the column having spacial characters in mysql

How to display the column desc order when the column having spacial chars in mysql
I am using the follow query but not display correctly
SELECT quotation_pno FROM crm_quotation order by quotation_pno desc
My output coming like this
quotation_pno
PT/17/999
PT/17/1533
PT/17/1532
PT/16/1531
I want my output like this
quotation_pno
PT/17/1533
PT/17/1532
PT/17/999
PT/16/1531
Please help me
I'd argue, that the output is correct, but your assumptions are not. It looks to me, as if quotation_pno is some kind of textual column, right?
The sorting assumes, that you want to sort text and this works this way:
Set i to 0
Compare the i-th character of two strigns
If they are the same and the end is not reached, increase i by 1 and proceed with step 2
Otherwise order the two strings according to the value at the i-th position
(There are some things elided and the pseudocode is boiled down to the very basic, needed to understand the principle).
Applied to your example this means, when the comparison compares PT/17/999 and PT/17/1533 it looks at the characters 0 to 5 and "sees" that they are equal. When it compares the characters at position 6, they are '9' and '1'. Since the character '9' is considered to be greater than '1', PT/17/999 is placed before PT/17/1533.
How to solve the issue?
There are some ways coming into my mind, that will allow you to achieve the desired sort order.
First, you could prepend the numbers with zeros. This will allow you to re-use most of your existing structure, but will result either in very many zeros, or a system that is somehow limited, since you will be restricted to the number of digits you decided to use (or the sort will fail again).
The second possibility is, to store the parts in (additional) numerical columns in the table, e.g. one for year and one for the order number in this year. This is the more flexible approach, but involves more changes.

How to Stack mysql query results to select one row after the other [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
please how can i query the database table one row at a time without repeating the same row until all the rows has been selected.
E.g
I want the query to select the first row the first time and second row the second until all the rows is selected. Note i don't want to use random selection has result may repeat itself.
Thank you in advance
mysql has an OFFSET option that will work perfectly for what you want to (not that i understand why you are doing what you want to do) do. eg
$offset = 0;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 1
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 2
I think in your case you would chuck it in a loop. I'm just showing it as above to illustrate the concept. Or maybe you will pass $offset as a parameter. Either way this should give you a good idea
What does offset do? It basically ignores the records before the offset count. so if i have records 1,2,3,4,5 and I select from this list with an offset of 2 I will get 3,4,5 as my list.
W3 Will explain it better than me
So this question is regarding an API request and response
Your response to the first api request should contain last_offset. Tell your consumer to pass offset as a parameter on their request. my.api.com?offset=1 after validation and sanitation of the parameter offset, you can include the value in your query else use default offset of 0
If they received last_offset=2 in the last response they should be passing offset=3 on the next request.
One way is make the primary key of the table a sequence of numbers starting from an arbitrary number.
Get hold of the starting index.
While writing the sql query,just write a loop and make the where=id + 1.This will give you each row at a time.

how to get best time from table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get best time from MySql table collumn which I made varchar (I can change it, just I didn't know better) which data looks like "00:05:22.22" (it's "hours:minutes:seconds.milliseconds").
it's like laps times in that collumn..
or I should change my DB table structure? to what?
and how I should do it then?
i.e.
in that collumn are records like this:
00:00:04.99
00:00:04.57
00:00:04.55
00:00:04.58
00:00:03.36
And I would like to get all of them ordered by shortest time. Like best lap.
If you are using MySQL 5.6.4 or greater, you can use the TIME datatype for your column and indicate that it should accept fractional parts of a second - see here.
Otherwise, using a VARCHAR column, you can still use the MIN/MAX functions for your data and expect to get a valid result (provided all values are correctly formatted), since alphabetically sorting the data in your use case should give the same result as numerically sorting it.
Alternatively, you can use the ORDER BY clause on that column and take just the first n results. Depends on your needs.
To get an ordered set from a table your query needs to specify an order -
SELECT *
FROM `table`
ORDER BY `lap_time` ASC
You do not have to include ASC in the query as any ORDER BY will sort from smallest to largest unless otherwise told not to do so with DESC
As you have said, your column datatype is varchar, you can do
select [column_names] from [table_name] order by [column_name_containing_time] asc
You can see an example here: SQL Fiddle

php numbers sort from mysql asc [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to get order numbers by ASC.
In my mysql table It written with - (ex. 1-2; 2-3; 15-60 etc.)
Now I get order:
15-60; 1-2; 2-3;
why not 1-2; 2-3; 15-60?
I'm making new sorting in my opencart.
Tutorial to make new sorting I used: sort by size opencart
You can try with:
SELECT *
FROM yourtable
ORDER BY numbers + 0
to automatically cast you VARCHAR column to a number, and apply the order correctly.
Please see fiddle here.
Assuming that your order numbers are in fact strings of digits interspersed with hyphens, you are getting the string sort order by default.
You can cast strings to integers, but perhaps a better approach is to store order numbers appropriately padded by zero digit characters, so that 15-60, 1-2, 2-3 are stored as 015-060, 001-002, 002-003 (or whatever degree of padding is appropriate). Then the string sort order and numeric sort order will be the same, and you don't need to do casting.
you can also use this
SELECT *
FROM yourtable
ORDER BY CAST(numbers as unsigned)
DEMO HERE

Categories