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

  • Home
  • SEARCH
  • 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 8707325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:50:35+00:00 2026-06-13T03:50:35+00:00

Somewhere in my application, I have a query: $sql = SELECT * FROM table

  • 0

Somewhere in my application, I have a query:

$sql = SELECT * FROM table ORDER BY column1;

I pass this query to my class, which generates a table:

$table = new HtmlTable($sql)

This $table object has a method ‘groupBy’, which should alter the $sql string and it should add second column in the ORDER BY clause, so the query should look like:

$sql = SELECT * FROM table ORDER BY added_column, column;

I tried this:

select * from (SELECT * FROM table ORDER BY column1) innerSQL ORDER BY innerSQL.column2

but it does not sort correctly ..

Actually, what I need is this:

$sql = SELECT * FROM table ORDER BY column2, column1;

But i don’t know, how can I alter the $sql string inside the HtmlTable class ..

Any ideas?

  • 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-13T03:50:37+00:00Added an answer on June 13, 2026 at 3:50 am

    You can use a regular expression to do it. Here’s an example of how your method could look like:

    protected function groupBy($sql) {
      return preg_replace('/ORDER BY (.*)/i', 'ORDER BY SomeField, $1', $sql);
    }
    

    What it does is replacing “ORDER BY ” with “ORDER BY , SomeField”. Here’s an example to test it:

    $sql = 'SELECT * FROM table ORDER BY column1';
    $sql = preg_replace('/ORDER BY (.*)/i', 'ORDER BY SomeField, $1', $sql);
    echo $sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somewhere along the line from the DB to the application, this: sauté is getting
We have a .NET application which targets .NET 3.5. Our clients run it from
SQL beginner here. I have a query which takes around 10 seconds to run,
SQL Server 2005. In our application, we have an entity with a parent table
Some where in the application there is an autogenerated query like SELECT DISTINCT `Name`,`Number`
Somewhere some guy said (I honestly do not know where I got this from),
I have an application that uses a nested set model class to organise my
I have application with this code: Module Startup <STAThread()> _ Public Sub Main() Try
I have a report window which shows the results returned from a potentially long
i have application with one form, which i developed in c#. I would like

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.