Is there a possibility to change the order of a string with numeric value - mysql

I have some strings in my database. Some of them have numeric values (but in string format of course). I am displaying those values ordered ascending.
So we know, for string values, 10 is greater than 2 for example, which is normal. I am asking if there is any solution to display 10 after 2, without changing the code or the database structure, only the data.
If for example I have to display values from 1 to 10, I will have:
1
10
2
3
4
5
6
7
8
9
What I would like to have is
1
2
3
4
5
6
7
8
9
10
Is there a possibility to ad an "invisible character or string which will be interpreted as greater than 9". If i put a10 instead of 10, the a10 will be at the end but is there any invisible or less visible character for that.
So, I repeat, I am not looking for a programming or database structure solution, but for a simple workaround.

You could try to cast the value as an number to then order by it:
select col
from yourtable
order by cast(col AS UNSIGNED)
See SQL Fiddle with demo

You could try appending the correct number of zeroes to the front of the data:
01
02
03
..
10
11
..
99

Since you have a mixture of numbers and letters in this column - even if not in a single row - what you're really trying to do is a Natural Sort. This is not something MySQL can do natively. There are some work arounds, however. The best I've come across are:
Sort by length then value.
SELECT
mixedColumn
FROM
tableName
ORDER BY
LENGTH(mixedColumn), mixedColumn;
For more examples see: http://www.copterlabs.com/blog/natural-sorting-in-mysql/
Use a secondary column to use as a sort key that would contain some sort of normalized data (i.e. only numbers or only letters).
CREATE TABLE tableName (mixedColumn varchar, sortColumn int);
INSERT INTO tableName VALUES ('1',1), ('2',2), ('10',3),
('a',4),('a1',5),('a2',6),('b1',7);
SELECT
mixedColumn
FROM
tableName
ORDER BY
sortColumn;
This could get difficult to maintain unless you can figure out a good way to handle the ordering.
Of course if you were able to go outside of the database you'd be able to use natural sort functions from various programming languages.

Related

Update a row if a field is a subsequence of a string

I have a string S = "1-2-3-4-5-6-7-8"
This is how my database table rows look like:
id
SubSequence
1
1-2-4-5
2
1-3-4-5
3
2-5-7-8
4
5-8-9-10
5
6-7-10-11
and so on ...
I want to write a query that would update (in this example) only the first 3 rows because they're a subsequence of string S.
The current solution I have is to programmatically go thru each row, check if it's a subsequence, and update. But I'm wondering if there's a way to do it at the MySQL level for performance.
Update: I don't mind changing the way data is stored. For example, String S could be an array holding those numbers, and the "SubSequence" column can hold those numbers as an array.
No, there is not a way to do the query you describe with good performance in SQL when you store the subsequences as strings like you have done. The reason is that doing substring comparisons cannot be optimized with indexes, so your query will be forced to do the comparisons row by row.
In general, when you try to store sets of values as a string, but you want to use SQL to treat them as discrete values, it's bound to be awkward, difficult to code, and ultimately have bad performance.
In this case, what I would do is make a two tables, one that numbers your entities, and a second table in which each value in your subsequence is stored on a row by itself.
SubSequences:
id
1
2
SubSequenceElements:
id
SubSequenceElement
1
1
1
2
1
4
1
5
2
1
2
3
2
4
2
5
And so on.
Then you can use relational-division techniques to find cases where every element of this set exists in the set you want to compare it to.
Here's an example:
SELECT s.id
FROM SubSequences AS s
LEFT OUTER JOIN (
SELECT id
FROM SubSequenceElements
WHERE SubSequenceElement NOT IN (1,2,3,4,5,6,7,8)
) AS invalid USING (id)
WHERE invalid.id IS NULL;
In other words, you want to return rows from SubSequences such that no match is found in SubSequenceElements with an element value that is not in the set you're trying to match.
It's a bit confusing, because you have to think about the problem is a double-don't-match-this-set problem. But once you get relational division, it can be very powerful.
If the set can be represented by the numbers 0 through 63 (or some subset of that), then...
Using a column like this
elements BIGINT UNSIGNED NOT NULL DEFAULT '0'
Then "2-5-7-8" could be put into it thus:
UPDATE ...
SET elements = (1<<2) | (1<<5) | (1<<7) | (1<<8);
Then various operations can be done in a single expression:
WHERE elements = (1<<2) | (1<<5) | (1<<7) | (1<<8) -- Test for exactly that set
WHERE (elements ^ ~ ( (1<<2) | (1<<5) | (1<<7) | (1<<8) )) != 0
-- checks to see if any other bits are turned on
This last example is close to what you need. One side of the "and not" would have the 1..8 of your example, the other would have
Your example has S represented as 0x1FE;
WHERE subsequence & ~0x1FE
will be 0 (false) for ids 1,2,3; non-zero (true) for ids 4 and 5.

Sort Numeric & Alphanumeric Accordingly in PHP MySQL

I am using a Query which sorts like;
1
2
3A
3
4A
4
But I want to get it
1
2
3
3A
4
4A
What I am using is
SELECT * FROM `sections` ORDER BY CAST(`act` as DECIMAL) ASC
What I exactly want is First Number then Alphanumeric
3 then 3A
Considering that you'll always have integer first and then characters in your acts column. Normally this is called Natural Sorting.
Problem Understanding:
Normal sorting works perfectly when we're dealing with variations of a single number i.e. 1, 1A, 1B, 1AB...
Due to string type, it fails when we sort between numbers which starts with same digit like 1, 10, 100....
We can sort in following manner.
First sort them based on their integer values.
Then sort them normally which mysql takes care by default.
SELECT * FROM sections
ORDER BY CAST(acts AS UNSIGNED), acts
Check corresponding fiddle. It is working for MySQL 5.5, 5.6, 5.7 & 8.0.

database schema one column entry references many rows from another table

Let's say we have a table called Workorders and another table called Parts. I would like to have a column in Workorders called parts_required. This column would contain a single item that tells me what parts were required for that workorder. Ideally, this would contain the quantities as well, but a second column could contain the quantity information if needed.
Workorders looks like
WorkorderID date parts_required
1 2/24 ?
2 2/25 ?
3 3/16 ?
4 4/20 ?
5 5/13 ?
6 5/14 ?
7 7/8 ?
Parts looks like
PartID name cost
1 engine 100
2 belt 5
3 big bolt 1
4 little bolt 0.5
5 quart oil 8
6 Band-aid 0.1
Idea 1: create a string like '1-1:2-3:4-5:5-4'. My application would parse this string and show that I need --> 1 engine, 3 belts, 5 little bolts, and 4 quarts of oil.
Pros - simple enough to create and understand.
Cons - will make deep introspection into our data much more difficult. (costs over time, etc)
Idea 2: use a binary number. For example, to reference the above list (engine, belt, little bolts, oil) using an 8-bit integer would be 54, because 54 in binary representation is 110110.
Pros - datatype is optimal concerning size. Also, I am guessing there are tricky math tricks I could use in my queries to search for parts used (don't know what those are, correct me if I'm in the clouds here).
Cons - I do not know how to handle quantity using this method. Also, Even with a 64-bit BIGINT still only gives me 64 parts that can be in my table. I expect many hundreds.
Any ideas? I am using MySQL. I may be able to use PostgreSQL, and I understand that they have more flexible datatypes like JSON and arrays, but I am not familiar with how querying those would perform. Also it would be much easier to stay with MySQL
Why not create a Relationship table?
You can create a table named Workorders_Parts with the following content:
|workorderId, partId|
So when you want to get all parts from a specific workorder you just type:
select p.name
from parts p inner join workorders_parts wp on wp.partId = p.partId
where wp.workorderId = x;
what the query says is:
Give me the name of parts that belongs to workorderId=x and are listed in table workorders_parts
Remembering that INNER JOIN means "INTERSECTION" in other words: data i'm looking for should exist (generally the id) in both tables
IT will give you all part names that are used to build workorder x.
Lets say we have workorderId = 1 with partID = 1,2,3, it will be represented in our relationship table as:
workorderId | partId
1 | 1
1 | 2
1 | 3

sorting numbers stored in text field in MYSQL database

i have a mySQL database with a text field in which is stored a number.
i need to produce a recordset sorted in descending numerical order.
this works fine until we get to numbers greater than 10 ie
9
8
7
6
5
4
3
2
10
1
is there a simple way of sorting this 'correctly' ?
(yes, i know i should have numbers in a numerical field, but i'm working with what i have :))
i'm using the results on an asp/vbscript/jquery page so maybe even a client-side solution is viable...
any suggestions?
ORDER BY ABS(text_column) DESC
Or, if you also have to deal with negative values:
ORDER BY CAST(text_column AS SIGNED) DESC
You need to type cast it to INTEGER using CAST function in MySQL:
ORDER BY CAST(text_column AS UNSIGNED INTEGER)
Try this one -
... ORDER BY text_column * 1

Grade Up/Down APL order

How come that
⌽(⍒'Hello')
is
1 2 4 3 5
when
⍋'Hello'
is
1 2 3 4 5
?
I'm new to APL and stumbled on it by accident. I just wonderes why the second l comes before the first.
You are using both the grade up ⍋ and grade down ⍒ as monadic primitives.
By definition grade up returns an integer array of indices which specify the sorted order of the expression following it, in ascending order. If any elements are equal (in your example the two letter l's) , they will appear in the result in the same order that they appeared in the input expression.
So, ⍋'Hello' returns 1 2 3 4 5. The two l's are in the same order, i.e., the 3rd character (1st letter l) precedes the 4th character (2nd letter l).
By definition grade down also returns an integer array of indices which specify the sorted order of the expression following it, in descending order. If any elements are equal (in your example the two letter l's) , they will also appear in the result in the same order that they appeared in the expression.
So, ⍒'Hello' returns 5 3 4 2 1. The two l's remain in the same order because they are equal.
When you apply rotate ⌽ the integer array gets reversed to 1 2 4 3 5 as you witnessed.
The outcome you are seeing is precisely what is expected given the way the functions are defined and how they deal with equal values.
If you want to see a more extreme example compare the output for the following two arrays. Create an array with 10 elements each having the same value of 1. 10⍴1 and then try the grade up function and then try the grade down function:
⍋10⍴1
and
⍒10⍴1
They will both yield the same result:
1 2 3 4 5 6 7 8 9 10
The grade up ⍋ and grade down ⍒ primitives preserve the order of equal elements. As others have said, there must be a rule for equal arguments. But this rule has the virtue that it allows multi-key sorts.
That is, if you have an array with several associated keys, by sorting on each key from least significant to most significant, you obtain a result sorted by the most significant key, with equals sorted by the 2nd mot significant, items equal on the 1st two sorted by the 3rd, and so on. For this to work the index vector must be captured and used to update all keys and the data to keep them in sync. Or they could be stored in a nested structure, in which case they would automatically be kept in proper relative order.