0

I have a table that has a bunch of rows, but only three columns, date, username and posts.

Username has usernames, and they repeat a lot. Posts has a bunch of numbers. Date has the date something was posted in the Y-m-D format.

Now when I run SELECT * FROM table WHERE date = '2014-02-20', I get a bunch of mixed results, like this:

date                       username                    posts
2014-02-20                 user1                       1
2014-02-20                 user2                       2
2014-02-20                 user14                      1
2014-02-20                 user3                       1
2014-02-20                 user2                       3
2014-02-20                 user2                       4
2014-02-20                 user11                      1
2014-02-20                 user1                       2
2014-02-20                 user8                       2
2014-02-20                 user9                       3
2014-02-20                 user55                      4
2014-02-20                 user5                       3

I want to sort it out so it will look like this:

date                       username                    posts
2014-02-20                 user1                       1
2014-02-20                 user1                       2
2014-02-20                 user1                       3
2014-02-20                 user1                       4
2014-02-20                 user2                       1
2014-02-20                 user2                       2
2014-02-20                 user2                       3
2014-02-20                 user2                       4
2014-02-20                 user2                       5
2014-02-20                 user2                       6
2014-02-20                 user3                       1
2014-02-20                 user3                       2

How can I do that?

1
  • There's just a problem with both answers. user10 comes right after user1, user2 comes after user19, and so on.. Commented Feb 20, 2014 at 7:54

3 Answers 3

1

Try like

SELECT * FROM table WHERE date = '2014-02-20' ORDER BY username,posts ASC
Sign up to request clarification or add additional context in comments.

2 Comments

I'll assign you the best answer because you answered in just 25 seconds, thanks.
I think user55 will come before user6 in this case
1
SELECT * FROM tablw WHERE date ='$something' ORDER BY username ASC, posts ASC

4 Comments

I think user55 will come before user6 in this case
@PrasanthBendra correct, I did that and user10 came right after user1.
And before user2 rite ?
Yes, but that happened with both cases, ORDER BY username,posts ASC and ORDER BY username ASC, posts ASC.
0

Try this :

SELECT *,CONVERT(SUBSTRING(username, 5, 9),UNSIGNED INTEGER) AS userord FROM table WHERE date = '2014-02-20' ORDER BY userord ASC, posts ASC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.