Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2812
  • Last Modified:

mysql sort by a column

I have a table that stores comment info for a message board.  The table has comment_body, id, and commenter_id in it.  there is another table with all the members that can post comments (member_id, member_name).  I want to be able to sort a question by the commenter name (not id).  I can't figure out how to sort the table alphabetically by info that is stored in another table.

if it is a complicated solution, I will raise the points.

1 Solution
Hi mattjp88,

You should be able to sort in sql, just "select comment_body, member_name from comment_table, member_table where member_table.member_id = comment_table.commenter_id order by member_name".

try this,

you can sort member_name as,

select comment_body, member_name from comment_table, member_table where member_table.member_id = comment_table.commenter_id order by member_name DESC;
I think most of time alphabetically means ascending order.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

You COULD read the entire array of comments in to an array, and while doing that, do lookups on the user names.

So it would be like...

while ($comment = mysql_fetch_row($comments))
   $username = mysql_fetch_row(mysql_query("select `Name` from `users` where `ID` = '$comment[2]' limit 1",$db))

Put all of the comment data in to a single line of information, seperated by something so you can explode it later.
Replace the IDs with the names, make the names the first items in the line, and do a sort() on the array...

It's a little more complicated than doing the ordering in the SQL statement as shown above, but it's another way to do it. :)
mattjp88Author Commented:
Well, actually, I am going to be sorting alphabetically either A-Z or Z-A, whichever the user selects.  Thanks for the code!

the code is exactly like inq's, points them them.  Yours does work however.  thanks!

Good alternate approach, although it isn't as efficient as doing it via the DB.

Thanks all!
Please try this this may be helpful to you

Select c.comment_body, m.memeber_name from comment_table as c, member_table as m where c.comment_id=m.member_id order by m.member_name;

Keep the charcter fields in appropriate syntax.
This query is fast enough to get the desired resuts.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now