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
Related
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.
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
I need to create a table for product management in that table field include like price , image , discount so any one can help me that which datatype is used for all of them.
It isn't a simple question as it seems at first look. I suggest you to read first how floats is implemented in different databases and programming languages. It will help you understand why you never shoud use floats to sotre money.
This article Always use decimal for money may help you.
Well you just have to ask yourself what the data is.
If its words of fixed length its CHAR(n) ( such as a SKU number), VARCHAR(n) is variable length up to n, TEXT is for things longer such as a description.
If it's money, like a price, it's a float, double, or decimal in the database ( they are all essentially the same thing, depends on your DB ). If it's a whole number it's an integer. For an image, if it's the link ( the src ) then use a VarChar, but for image data itself its a BLOB.
Words "hello" = strings, varchar, char, text
Numbers "1" = integers
floats "1.01" = float, double, decimal etc.
binary data - such as images its called a BLOB or Binary Large Object
lists such as a status, "paid, due, late" you could use a ENUM type field.
Here is some reading meterial on types in MySql.
https://dev.mysql.com/doc/refman/5.0/en/data-type-overview.html
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
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 8 years ago.
Improve this question
I've done a system for a client where they can nominate colleagues for doing a great job. Each person has 12 nominations to give annually.
So I've got a database with a table nominations which stores, id, nominator(id), nominated(id), reason and date.
I've also got a table user which stores user data such as, total nominations given, received, id, email etc.
So I'm creating a page from which you can pull reports.
Here you can choose a start date and end date and amount of records you would like.
How would the SQL query look to determine who made the most nominations between the specified dates?
I'm not a SQL guru at all...so any help would be appreciated very much.
After some research I've managed to find out COUNT(*) is the way to go...but don't want to run a query for every user that nominated between the specified dates...and sorting it this way could be a problem.
Please any help would be great.
select nominator, count(*)
from yourTable
where nominatedDate >= '1 Jan 2013' and nominatedDate <= '31 Jan 2013'
group by nominator
When you do aggregation functions (like COUNT. MIN, MAX, AVG) you either need to apply them to every row selected, which will give just one row in the output, or to GROUP BY items you want to make into sub-totals. In this case, for each value of Nominator in the table we get the Nominator value, and the count of rows containing that value.
The Where clause limits the counted rows to those where nominatedDate is in the given range. You can put AND and OR other tests (its already got one and).
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 9 years ago.
Improve this question
040, 044P, 041BL, 041W, 041PB
^^ This is the order it is coming out in by using Order By clause.
I think this is how it should appear instead:
040, 041BL, 041PB, 041W, 044P
I know similar questions must have been asked before, but I still can't figure out anything!
Edit: After X.L.Ant's comment, I realized my mistake. Therefore, simple order by clause is working for the test case given above. However, the case is still complicated if the number of digits is not always going to be 3 as GolezTrol mentioned. What should one do in that case?
Try:
SELECT string,
#num := CONVERT(string, signed) AS num_part,
Substring(Trim(LEADING '0' FROM string), Length(#num) + 1) AS rest_of_string
FROM table1
ORDER BY num_part,
rest_of_string
This way, the numbers will still be ordered by their numerical value (the leading 0s not being taken into account).
See fiddle.
try LPAD() function - but remeber that LPAD trims digits.
http://sqlfiddle.com/#!2/d7281/3/0