Question

In my admin interface I have a listing of subdomains, which follow following logic:

node-k1
node-k2
node-k3
node-k4
node-k5
node-k6
node-k7
node-k8
node-k9
node-k10
node-k11

These are stored in mysql. I need to list them in numeric order, however since I lack leading 0s after node-k, it orders them like so:

node-k1
node-k10
node-k11
node-k2
node-k3
etc

There nodes have different letters, so node-j_ would appear before node-k_. ORDER BY node ASC works fine with the letters obviously, however the numbers are an annoying issue.

Is there any way to make the ORDER BY clause put them in a right order? This is an admin only function, so performance isn't really an issue.

Was it helpful?

Solution

If the number always starts at character 7, then you can cast it to an integer like this:

SELECT *
FROM tbl
ORDER BY SUBSTR(subdomain, 1, 6), CAST(SUBSTR(subdomain, 7) AS UNSIGNED)

This orders them first by the prefix, then by the number.

OTHER TIPS

in your table of where the nodes were recorded, there must be a numeric field(unique) which basically refers to the order of your nodes.

ID NodeName
1 Node-k1
2 Node-k2
3 Node-k3
4 Node-k10
5 Node-k11

select Nodename from table1 order by ID.

Rhian

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top