Skip to content
Advertisement

MySQL query returns columns in different order after update

One of my programs was using this query to get the details of a selected table’s columns:

JavaScript

The result used to be:

array(36) { [0]=> array(7) { [“column_name”]=> string(2) “id” [“column_type”]=> string(7) …

The first column in that table is id, and in the results array, the first index was id (as it’s supposed to be).

Now, however, after updating PHP (from 5.6 to 7.3) and MySQL, the results are sorting differently.

It is not sorting based on column’s creation date priority and is instead sorting based on some unknown element (id is 3rd index in array).

Is this a bug or something? How can I get the original sorting order back?


Okay, I just figured something out. MySQL is sorting the columns with a foreign keys first, and then the rest of the columns.

I don’t want this! How can I get the original sort order back?


Here are my results. First is the current (wrong) output, then the expected output.

Incorrect output

JavaScript

Here it is their real position in MySQL: (I changed some of the names for personal reasons)

Expected output

JavaScript

As you can see, MySQL sorted all the columns with references at the top of the results.

I want to get them in their real position with those details.

The new result is not logical anyway. I would really appreciate it if you could explain to me what is happening here.

Advertisement

Answer

I don’t know why your output changed, but I think that to get the output you want, you can use an ORDER BY clause to order by the ordinal position.

JavaScript

When I ran your query on one of my own MySQL databases, I added a c.* to your list of SELECTed columns. From all the extra columns displayed, the ORDINAL_POSITION seems like it might be what you are looking for.

So your final query might looks something like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement