Mysql

I have a mysql table called schedules with a field called hour which is a char(2) and one called sequence which is int(8).  I would like to do

SELECT * FROM schedules ORDERED BY hour AND sequence;

When I use the above command the hour sort is not correct because it is sorting as a char field as opposed to integer and the sequence sort is highest number first.  I need What do I need to change or do.  Thanks!
sargent240Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Try this...
SELECT * FROM schedules ORDER BY CAST(hour AS UNSIGNED) AND sequence;

Open in new window

0
Cornelia YoderArtistCommented:
The most obvious thing to do is change the hour field to int(2).

If the hour field always has a 2-digit value from 00 to 24, then you could sort it in numeric order by using ASC or DESC.  

You can ORDER BY hour DESC, sequence ASC  or whatever ordering you want.  Point is that the two fields can be sorted separately.

If the hour field sometimes has a 2-digit number and sometimes a 1-digit number, I fear you have a big problem.  You could maybe add a leading 0 in the latter case.
0
Dave BaldwinFixer of ProblemsCommented:
Or try this...
SELECT * FROM schedules ORDER BY CAST(hour AS UNSIGNED) ASC, sequence ASC;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sargent240Author Commented:
David, the command you suggested works fine.  The only problem I have is they are descending and I need both to be ascending.  I tried this but it gave me an error.

SELECT * FROM schedules where scheduleId = '1662' ORDER BY CAST(hour AS UNSIGNED) ASC AND sequence ASC;

If I only do a sort on the hour it works fine and is ascending.  What do ya think.
0
sargent240Author Commented:
David.  Your second post did things just as I need them.   THANKS!
0
sargent240Author Commented:
Perfect
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.