Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:26:25+00:00 2026-06-15T05:26:25+00:00

I need to order some information with order by in a MySQL query. Is

  • 0

I need to order some information with order by in a MySQL query. Is there a possibility to use the ABS function only if the first char is numeric?

Pseudo example:

SELECT * FROM table 
ORDER BY
(( name[0] = numeric ) ? ABS(name) : name)
ASC

Thanks for the answers so far, tried a lot with your answers but I don’t get the result I want.

The current code:

<?php
$filters = $this->db->query("
    SELECT
        tb_spec_feature.feature_id,
        tb_spec_feature.name
    FROM
        tb_spec_feature
    WHERE
        filter=1
");
$filters = $filters->result_array();
foreach($filters as $key=>$filter)
{
    $filters_sub = $this->db->query("
        SELECT DISTINCT
            tb_spec_value.value_id,
            tb_spec_value.name as value_name,
            tb_spec_sign.name as sign_name
        FROM
            tb_spec_fk
        INNER JOIN
            tb_spec_value
        ON
            tb_spec_value.value_id = tb_spec_fk.value
        LEFT JOIN
            tb_spec_sign
        ON
            tb_spec_sign.sign_id = tb_spec_fk.sign
        WHERE
            feature=".$this->db->escape($filter['feature_id'])."
        ORDER BY
            ABS(tb_spec_value.name) ASC
    ");
    $filters[$key]['values'] = $filters_sub->result_array();
}
?>

This will output (tb_spec_feature.name and then the tb_spec_value.name):

Screen inches

  • 7
  • 7.7
  • 7.9
  • 8.9
  • 9.7
  • 10
  • 10.1
  • 12.1

Resolution

  • 800 x 480
  • 1024 x 600
  • 1024 x 768
  • 1280 x 800
  • 1280 x 768
  • 1920 x 1200
  • 2048 x 1536

Operating system

  • Android 3.1
  • Android
  • BlackBerry Tablet OS
  • Android 3.2
  • Android 2.3
  • Android 4.0
  • iOS 5
  • Android 4.0.3
  • iOS 4
  • Android 2.2
  • Windows 7 Professional
  • Windows 8
  • Windows 8 Pro
  • iOS 6

As you can see the inches and resolutions are okay, but the operating systems aren’t ordered well. If I remove the ABS the result will be:

Screen inches

  • 10
  • 10.1
  • 12.1
  • 7
  • 7.7
  • 7.9
  • 8.9
  • 9.7

Resolution

  • 1024 x 600
  • 1024 x 768
  • 1280 x 768
  • 1280 x 800
  • 1366 x 768
  • 1920 x 1200
  • 2048 x 1536
  • 800 x 480

Operating system

  • Android
  • Android 2.2
  • Android 2.3
  • Android 3.1
  • Android 3.2
  • Android 4.0
  • Android 4.0.3
  • BlackBerry Tablet OS
  • iOS 4
  • iOS 5
  • iOS 6
  • Windows 7 Professional
  • Windows 8
  • Windows 8 Pro

As you can see now, the operating systems are ordered well but the inches and resolutions not.. I hope somebody can help me out 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:26:26+00:00Added an answer on June 15, 2026 at 5:26 am

    This code is the MySql way of your question:

    SELECT * FROM table 
    ORDER BY
    CASE WHEN LEFT(name, 1)>='0' AND LEFT(name, 1)<='9' THEN ABS(name) 
         ELSE name 
         END ASC
    

    (Edit)
    But according to comments, is better for you:

    ORDER BY ABS(name), name
    

    That works in this way:
    If ABS() can be applied to ‘name’ the numeric resulting value will be ordered, but in the opposite case, if ABS(name) evaluates to null (because a non numeric has no abs value) then the second field (name) on order by, is which is relevant to the sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to set some varaibles i need the information if a given file
When I need to retrieve some information in Order table, jasper cannot cast from
I need some information from a website that's not mine, in order to get
I need some help on my mysql syntax. My table has user information but
I need some advice on which Swing Components to choose in order to achieve
In some of my tests i need to check the order of Lists and
I need to spoof the IP source address of some packets in order to
I have some things in my app that I need to store, in order
The below shown is my query I need to order by with these three
I am new to using prepared statements in mysql with php. I need some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.