Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
Ordering the columns of MySql table using Python

I'm beginner in SQL.

Consider my simple code:

cnx = mysql.connector.connect(user='root', password='',
                              host='127.0.0.1', database='employees')
cursor = cnx.cursor()

cursor.execute('SELECT * FROM people ORDER BY Height DESC, Weight')

my_result = cursor.fetchall()

cnx.close()

for x in my_result:
  print(x[0], x[1], x[2])

I have a people table in my employees database with columns name, Height and Weight. If I run this code, it prints the desired sorting and there's no problem. But, it seems that this code essentially doesn't change the order of rows of the table. I mean, now if I run

SELECT * FROM people;

in terminal I get the latter table (which does not have the desired order). Now, my question is:

How can I change the order of the table to obtain a new table with desired sorting using Python? Is it possible?

Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can create a new table like the old one and insert rows with the desired order, otherwise always run your query with ORDER BY.

To create a new table you can run a query similar to:

CREATE TABLE people_sorted LIKE people; 
INSERT people_sorted SELECT * FROM people ORDER BY Height DESC, Weight;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!